Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 575
Rafael Adel Avatar asked Jul 19 '26 15:07

Rafael Adel


1 Answers

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

  }
like image 113
heads5150 Avatar answered Jul 21 '26 05:07

heads5150



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!