LCJ*_*LCJ 1 .net asp.net asp.net-mvc razor
我需要使用带有RAZOR的MVC3将多个文件上传到Web服务器.我有以下代码.在控制器中,我的文件数为零.如何更正它以获取上传的文件的实际数量并获取内容?
public class MyFileController : Controller
{
public ActionResult MyFileProcessActionTest()
{
return View();
}
[HttpPost]
public ActionResult MyFileProcessActionTest(IEnumerable<System.Web.HttpPostedFileBase> files)
{
int fileCount = files.Count<System.Web.HttpPostedFileBase>();
return RedirectToAction("Index");
}
}
Run Code Online (Sandbox Code Playgroud)
视图
@{
ViewBag.Title = "MyFileProcessActionTest";
}
<h2>MyFileProcessActionTest</h2>
@using (Html.BeginForm())
{
<input type="file" name="files" id="file1" />
<input type="file" name="files" id="file2" />
<input type="submit" />
}
Run Code Online (Sandbox Code Playgroud)
读:
您必须enctype在form标记中包含该属性,以指示该表单应包含文件.
@using (Html.BeginForm("YourAction", "Controller", FormMethod.Post, new {enctype="multipart/form-data"))
{
}
Run Code Online (Sandbox Code Playgroud)
更改您的表单以匹配以下内容
@using(Html.BeginForm("action","controller",FormMethod.Post,new{encType = "multipart/form-data"})){
{
<input type="file" name="files[0]" id="file1" />
<input type="file" name="files[1]" id="file2" />
<input type="file" name="files[2]" id="file3" />
<input type="submit" />
}
Run Code Online (Sandbox Code Playgroud)
索引0,1,2将允许模型绑定器IEnumerable进一步encType必须在将文件发布到服务器时指定