deejay
deejay

Reputation: 11

upload multiple images using a single file upload control in asp.net web forms

how can I upload multiple images using a single file upload control in asp.net web forms?

I am able to upload a single image file using file upload control but I want to make it more dynamic to upload multiple images by using one control.

Can anyone help me out in this?

Thankyou.

Upvotes: 0

Views: 1590

Answers (2)

Karim Saleh
Karim Saleh

Reputation: 38

You need to use AllowMultiple attribute, like this

<asp:FileUpload id="controlID" runat="server" AllowMultiple="true"/>

Upvotes: 1

Gavin Ward
Gavin Ward

Reputation: 1022

You can't. It is strictly one file per control.

To upload multiple files using one submit button you would need to use Javascript to add more FileControls dynamically, like this (using jQuery):

$(document).ready(function () {

   $("#addAnotherFile").click(function () {
       $("input[type='file']").after('<br /><input type="file" name="file" />');
   }
});

In the submit button handler, you can then enumerate through the Request.Files collection to access your uploads:

for (int i = 0; i < Request.Files.Count; i++)
{
    HttpPostedFile file = Request.Files[i];
    if (file.ContentLength > 0)
    {

 file.SaveAs(Path.Join("Uploaded/Files/Path",Path.GetFileName(file.FileName)));
    }
}

Upvotes: 0

Related Questions