[英]upload files in asp.net mvc
I'd like to upload a file in asp.net mvc. 我想在asp.net mvc中上传文件。 I've the following codes. 我有以下代码。 But file
returns always null
value. 但是file
始终返回null
值。 Is there anything, I should try? 有什么我应该尝试吗?
.cshtml .cshtml
<input type="file" name="file" id="file" />
controller.cs 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");
}
添加到表单enctype="multipart/form-data"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.