简体   繁体   English

如何在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]" />

Then I would like to be able to receive these files as an array in the Controller. 然后我希望能够在Controller中将这些文件作为数组接收。 I've tried this. 我试过这个。

public ActionResult AddPart(HttpPostedFileBase[] files)

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#. 有谁知道如何使用MVC3 C#做到这一点。

If you want to upload not only one file, you need to use enctype="multipart/form-data" in your form. 如果您不仅要上传一个文件,则需要在enctype="multipart/form-data"使用enctype="multipart/form-data"

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

And controller: 和控制器:

[HttpPost]
public ActionResult AddPart(IEnumerable<HttpPostedFileBase> files) 

All other parts is ok. 所有其他部分都可以。

Well i got almost a same case. 好吧,我得到几乎相同的情况。 But that is for nested array of files. 但这适用于嵌套的文件数组。

using IEnumerable as an array( [ ] ) solved my problem. 使用IEnumerable作为数组( [] )解决了我的问题。 [] s [] s

[HttpPost]
public ActionResult AddPart(IEnumerable<HttpPostedFileBase>[] files)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM