我在ASP.NET MVC中上传文件时遇到问题。 我的代码如下:HttpPostedFileBase总是在ASP.NET MVC中返回空值
查看:
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index2</h2>
@using (Html.BeginForm("FileUpload", "Board", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" />
<input type="submit" />
}
控制器:
[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase uploadFile)
{
if (uploadFile != null && uploadFile.ContentLength > 0)
{
string filePath = Path.Combine(Server.MapPath("/Temp"), Path.GetFileName(uploadFile.FileName));
uploadFile.SaveAs(filePath);
}
return View();
}
但uploadFile总是返回null。 任何人都可以找出原因吗?
哇......非常感谢。我不知道这个MVC。我对ASP.NET MVC很陌生。谢谢。 – 2011-12-18 13:29:27
我没有错过字段名称,但由于缺少表单定义中的enctype参数而出现相同的'空'问题。感谢这个例子。 – niallsco 2012-04-02 08:54:17
@dotnetstep谢谢 – anpatel 2012-11-29 15:31:38