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