Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
622 views
in Technique[技术] by (71.8m points)

.net - 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. 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#.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...