Rafael Adel
Rafael Adel

Reputation: 7759

Using HTML5 multiple file upload with ASP.NET

I'm trying to upload multiple files using

<input id="testUpload" type="file" multiple="true"/>

(yes, i know it doesn't work on IE). But my question is what should i do after that in the code to iterate through each file and upload it ?

I'm trying

foreach(HttpPostedFile file in Request.Files["testUpload"]){

}

But i get

foreach statement cannot operate on variables of type 'System.Web.HttpPostedFile' because 'System.Web.HttpPostedFile' does not contain a public definition for 'GetEnumerator'

I know i can just do for multiple = "false" :

HttpPostedFile file = Request.Files["testUpload"];

And then do operation on that file. But what if i'm selecting multiple files ? How to iterate though each one using foreach ?

Upvotes: 3

Views: 9723

Answers (2)

Murad
Murad

Reputation: 11

Thank you, Thank you, Thank you. It saved my day.

However, I had to use HttpPostedFile instead of HttpPostedFileBase.

for (int i = 0; i < Request.Files.Count; i++)
{
    **HttpPostedFile** file = Request.Files[i];
    if(file .ContentLength >0){
    //saving code here
    }
}

Either way, this is great

Upvotes: 0

heads5150
heads5150

Reputation: 7443

You are trying to iterate over one file rather than the collection.

Change

foreach(HttpPostedFile file in Request.Files["testUpload"]){

}

to

EDIT - changed to for loop as per comment

for (int i = 0; i < Request.Files.Count; i++)
{
    HttpPostedFileBase file = Request.Files[i];
    if(file .ContentLength >0){
    //saving code here

  }

Upvotes: 13

Related Questions