0
我想在asp.net mvc上传一个文件。我有以下代码。但file
总是返回null
值。有什么,我应该尝试吗?在asp.net上传文件mvc
.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");
}
你可以分享查看表单代码? – 2013-05-13 08:17:18
我认为你的表单元素不包含'enctype =“multipart/form-data”' – 2013-05-13 08:19:00
看Darin的解决方案http://stackoverflow.com/questions/5193842/file-upload-asp-net-mvc-3-0/ 5193851#5193851 – 2013-05-13 08:21:12