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. 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#.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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"}))
And controller:
[HttpPost] public ActionResult AddPart(IEnumerable<HttpPostedFileBase> files)
All other parts is ok.
Method 2
Well i got almost a same case. But that is for nested array of files.
using IEnumerable as an array([ ]) solved my problem. [] s
[HttpPost] public ActionResult AddPart(IEnumerable<HttpPostedFileBase>[] files)
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0