如何在ASP.NET MVC 3中发布文件数组?

spa*_*cus 9 .net html asp.net post asp.net-mvc-3

我希望能够以一种形式发布多个文件.我想将这些文件作为文件数组传递.例如,我想这样做.

<input type="file" name="files[0]" />
<input type="file" name="files[1]" />
<input type="file" name="files[2]" />
Run Code Online (Sandbox Code Playgroud)

然后我希望能够在Controller中将这些文件作为数组接收.我试过这个.

public ActionResult AddPart(HttpPostedFileBase[] files)
Run Code Online (Sandbox Code Playgroud)

但这不起作用.我用谷歌搜索了它,但我能找到的只是上传一个文件的例子.有谁知道如何使用MVC3 C#做到这一点.

Ale*_*dih 5

如果您不仅要上传一个文件,则需要enctype="multipart/form-data"在表单中使用.

@using (Html.BeginForm("", "Client", FormMethod.Post, new {enctype="multipart/form-data"}))
Run Code Online (Sandbox Code Playgroud)

和控制器:

[HttpPost]
public ActionResult AddPart(IEnumerable<HttpPostedFileBase> files) 
Run Code Online (Sandbox Code Playgroud)

所有其他部分都可以.