1

I have an

<input type="file" id="files" name="files[]" multiple runat="server" />

I don't use asp:FileUpload because I need to use a jQuery plugin for multiple image previews and for deleting images before upload them.

My question is How can I manage input data from code behind? I have to loop through selected images. I searched on the web and I didn't find anything interesting...

If I try, from code behind to read in this way:

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

I see that Request.Files.Count is always 0.

Thanks in advance!

Martina
  • 1,852
  • 8
  • 41
  • 78
  • I imagine the data should still be in the `Request.Files` collection if it was posted as part of the form. Server-side controls or not, everything that's part of the HTTP request should be on `Request`. – David Jan 27 '17 at 22:43
  • I tried with HttpPostedFile file = Request.Files["files[]"]; and with HttpPostedFile file = Request.Files["files"]; but it's always null – Martina Jan 27 '17 at 22:51
  • When you debug, is there anything at all in `Request.Files`? Examine the type before trying to assign random indexes to things. – David Jan 27 '17 at 22:52
  • no, count of Request.Files is 0... – Martina Jan 27 '17 at 22:54
  • If `Request.Files` is always of length 0, are you certain the files are being posted in the first place? Check the network tab in your browser's debugging tools. Does the request include files at all? – David Jan 27 '17 at 23:09

1 Answers1

0

To Be Honest a quick search gave me this:

In your aspx :

<form id="form1" runat="server" enctype="multipart/form-data">
 <input type="file" id="myFile" name="myFile" />
 <asp:Button runat="server" ID="btnUpload" OnClick="btnUploadClick" Text="Upload" />
</form>

In code behind :

protected void btnUploadClick(object sender, EventArgs e)
{
    HttpPostedFile file = Request.Files["myFile"];

    //check file was submitted
    if (file != null && file.ContentLength > 0)
    {
        string fname = Path.GetFileName(file.FileName);
        file.SaveAs(Server.MapPath(Path.Combine("~/App_Data/", fname)));
    }
}

Update

Another Quick Search gave me this, if you have multiple files then the solution to get them all would be:

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

 }
Masoud Andalibi
  • 3,168
  • 5
  • 18
  • 44