如何在ASP.NET MVC 3中发布文件数组? [英] How to post an array of files in ASP.NET MVC 3?
问题描述
我希望能够以一种形式发布多个文件.我想将这些文件作为文件数组传递.例如,我想这样做.
I would like to be able to post multiple files in one form. I would like to pass these files as an array of files. For example I would like to do this.
<input type="file" name="files[0]" />
<input type="file" name="files[1]" />
<input type="file" name="files[2]" />
然后,我希望能够在Controller中将这些文件作为数组接收.我已经尝试过了.
Then I would like to be able to receive these files as an array in the Controller. I've tried this.
public ActionResult AddPart(HttpPostedFileBase[] files)
但这不起作用.我已经用谷歌搜索了,但是我能找到的只是关于上传一个文件的例子.有谁知道如何使用MVC3 C#做到这一点.
But that doesn't work. I've googled it but all I can find is examples on uploading one file. Does anyone know how to do this using MVC3 C#.
推荐答案
如果您不仅要上传一个文件,就需要在表单中使用enctype="multipart/form-data"
.
If you want to upload not only one file, you need to use enctype="multipart/form-data"
in your form.
@using (Html.BeginForm("", "Client", FormMethod.Post, new {enctype="multipart/form-data"}))
和控制器:
[HttpPost]
public ActionResult AddPart(IEnumerable<HttpPostedFileBase> files)
所有其他部分都还可以.
All other parts is ok.
这篇关于如何在ASP.NET MVC 3中发布文件数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!