【发布时间】:2013-03-18 19:40:16
【问题描述】:
我是 MVC 4 的新手,我正在尝试在 我的网站。我找不到错误。我得到一个空值 我的文件中的值。
控制器:
public class UploadController : BaseController
{
public ActionResult UploadDocument()
{
return View();
}
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Images/"), fileName);
file.SaveAs(path);
}
return RedirectToAction("UploadDocument");
}
}
查看:
@using (Html.BeginForm("Upload", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="FileUpload" />
<input type="submit" name="Submit" id="Submit" value="Upload" />
}
【问题讨论】:
-
你只需要改变 public ActionResult Upload(HttpPostedFileBase file)
-
在这里查看我的实现stackoverflow.com/a/40990080/4251431
-
错过了表单上的
enctype花了我一个小时 -
Upload() 方法和按钮的连接在哪里。应该有一个 onClick 事件吗?我是 asp.net 新手