0

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.

deejay
  • 11
  • 1
  • Please show what you've tried... any code or anything... – Arnab Mar 20 '18 at 09:02
  • Possible duplicate of [How to choose multiple files using File Upload Control?](https://stackoverflow.com/questions/17441925/how-to-choose-multiple-files-using-file-upload-control) – VDWWD Mar 20 '18 at 09:12

2 Answers2

1

You need to use AllowMultiple attribute, like this

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

  • Just wanted to add, the files posted using `AllowMultiple="true"` can be accessed in the code behind using `controlID.PostedFiles`. – centurion Apr 29 '21 at 07:41
0

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)));
    }
}
Gavin Ward
  • 1,022
  • 8
  • 12