在视图上:
<% =Html.BeginForm("About", "Home", FormMethod.Post, new {enctype="multipart/form-data "})%>
<input type="file" name="postedFile" />
<input type="submit" name="upload" value="Upload" />
<% Html.EndForm(); %>在控制器中,有类似这样的东西:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult About(HttpPostedFile postedFile)
{
//but postedFile is null
View();
}About()中的postedFile为空。如何上传文件?
发布于 2009-07-13 12:29:42
使用HttpPostedFileBase (而不是HttpPostedFile)并将参数命名为与表单中完全相同的名称。例如:如果你有
<input type="file" id="file1" name="file1" />你必须有方法的头部:
public ActionResult About(HttpPostedFileBase file1)发布于 2009-02-13 00:51:45
这并不能解释为什么您的参数为空,但您可以直接深入研究该请求。不过,这可能不是最"MVC“的方式。在你的方法体中尝试这样做:
var upload = Request.Files["postedFile"]
if (upload.ContentLength > 0)
{
// Do whatever
}为了更加"MVC“,您可以将该代码从控制器中提取到IModelBinder实现中,并使用自定义对象作为您的方法的参数。此Scott Hanselman blog post显示了实现自定义ModelBinder的步骤。
发布于 2015-08-26 00:06:46
使用这个
public ActionResult Upload(HttpPostedFileBase excelfile)更改HttpPostedFileBase的HttpPostedFile
https://stackoverflow.com/questions/544182
复制相似问题