我想在asp.net mvc上传一个文件。我有以下代码。但file
始终返回null
值。有什么,我应该试试?
.cshtml
<input type="file" name="file" id="file" />
controller.cs
[HttpPost]
public ActionResult Index(BookModel model, HttpPostedFileBase file, FormCollection values)
{
try
{
if (!ModelState.IsValid)
{
return View("Index", new BookModel());
}
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/img/"), fileName);
file.SaveAs(path);
model.ImageUrl = fileName;
}
//SendMail();
}
catch (Exception ex)
{
return View("Index", new BookModel());
}
return View("Success");
}
答案 0 :(得分:3)
添加到表单enctype="multipart/form-data"