这是我的代码。我想上传3个文件到我的数据库
首先在视图中我写道:<% using (Html.BeginForm(动作名,控制器,FormMethod.Post,新{enctype="multipart/form-data"})){%> .....……
这是3个文件上传:
<input type="file" name="files" id="FileUpload1" />
<input type="file" name="files" id="FileUpload2" />
<input type="file" name="files" id="FileUpload3" />在控制器中,我使用以下代码:
IEnumerable<HttpPostedFileBase> files = Request.Files["files"] as IEnumerable<HttpPostedFileBase>;
foreach (var file in files)
{
byte[] binaryData = null;
HttpPostedFileBase uploadedFile = file;
if (uploadedFile != null && uploadedFile.ContentLength > 0){
binaryData = new byte[uploadedFile.ContentLength];
uploadedFile.InputStream.Read(binaryData, 0,uploadedFile.ContentLength);
}
}但是这些文件总是返回NULL :(
请帮帮我,谢谢。
发布于 2010-11-20 18:56:49
试着这样做:
<% using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) {%>
<input type="file" name="files" id="FileUpload1" />
<input type="file" name="files" id="FileUpload2" />
<input type="file" name="files" id="FileUpload3" />
<input type="submit" value="Upload" />
<% } %>和相应的控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(IEnumerable<HttpPostedFileBase> files)
{
foreach (var file in files)
{
if (file.ContentLength > 0)
{
// TODO: do something with the uploaded file here
}
}
return RedirectToAction("Index");
}
}它更干净一点。
发布于 2018-05-17 21:13:09
您应该使用:
IList<HttpPostedFileBase> files = Request.Files.GetMultiple("files")而不是。
https://stackoverflow.com/questions/4232347
复制相似问题