Search code examples
htmlasp.net.netfile-uploadserver-side

asp.net/HTML file upload control not uploading mutiple files as expected


I am using asp.net file control. I am uploading multiple files. The problem is when I select two or multiple files, it's only upload one file multiple times. I mean if I select two different images, It will upload the first image two times. And if I select three images, then it will upload first image three times.

My file upload control is following,

<asp:FileUpload  runat="server" ID="file" multiple />

And I server side code is following

    protected void click(object sender, EventArgs e) {
    foreach (string s in Request.Files)
    {
        HttpPostedFile file = Request.Files[s];

        int fileSizeInBytes = file.ContentLength;
        string fileName = Request.Headers["X-File-Name"];
        string fileExtension = "";

        if (!string.IsNullOrEmpty(fileName))
            fileExtension = Path.GetExtension(fileName);

        // IMPORTANT! Make sure to validate uploaded file contents, size, etc. to prevent scripts being uploaded into your web app directory
        string savedFileName = Path.Combine(@"D:\Temp\", Guid.NewGuid().ToString() + ".jpg");
        file.SaveAs(savedFileName);
    }
}

I have no idea why this is behaving this way. When I debug the server side code, it's giving me different names of files from "Request.Headers["X-File-Name"]" but somehow it's uploading same content (the first image from the multiple images that I try to upload)


Solution

  • You can get posted files from file upload control if you are using ASP.NET > 4.0 like this

    foreach (HttpPostedFile f in file.PostedFiles)
            { 
            //HttpPostedFile file = Request.Files[s];
    
            int fileSizeInBytes = f.ContentLength;
            string fileName = f.FileName;
            string fileExtension = "";
    
            if (!string.IsNullOrEmpty(fileName))
                fileExtension = Path.GetExtension(fileName);
    
            // IMPORTANT! Make sure to validate uploaded file contents, size, etc. to prevent scripts being uploaded into your web app directory
            string savedFileName = Path.Combine(@"D:\Temp\", Guid.NewGuid().ToString() + ".jpg");
            f.SaveAs(savedFileName);
        }
    

    With ASP.NET 4.0 or less aslo available in 4.0 and above

    HttpFileCollection fc = Request.Files;
    
        for (int i = 0; i < fc.Count; i++)
        {
            HttpPostedFile f = fc[i];
            int fileSizeInBytes = f.ContentLength;
            string fileName = f.FileName;
            string fileExtension = "";
    
            if (!string.IsNullOrEmpty(fileName))
                fileExtension = Path.GetExtension(fileName);
    
            // IMPORTANT! Make sure to validate uploaded file contents, size, etc. to prevent scripts being uploaded into your web app directory
            string savedFileName = Path.Combine(@"D:\Temp\", Guid.NewGuid().ToString() + ".jpg");
            f.SaveAs(savedFileName);
        }