Tôi gặp sự cố khi tải lên tệp trong ASP.NET MVC. Mã của tôi là dưới đây:HttpPostedFileBase luôn trả về null trong ASP.NET MVC
Xem:
@{
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" />
}
Bộ điều khiển:
[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();
}
Nhưng UploadFile luôn trả về null. Bất cứ ai có thể tìm ra lý do tại sao ??
wow ... cảm ơn rất nhiều. Tôi không biết về MVC này. Tôi rất mới với ASP.NET MVC. Cảm ơn. –
Tôi đã không bỏ lỡ tên trường nhưng đã có cùng một vấn đề 'null' do thiếu tham số enctype trong định nghĩa biểu mẫu. Cảm ơn ví dụ. – niallsco
@dotnetstep thanks – anpatel