0

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)

1 Answer 1

1

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);
    }
Sign up to request clarification or add additional context in comments.

7 Comments

still uploading same file (first file) multiple times and ignoring other files
there is no property as file.PostedFiles. there is file.PostedFile but it does not return any collection. It returns single file related info. So it's giving error in foreach "foreach statement can not operate on variable file.PostedFile..."
There is a property PostedFiles dude.
there is fileupload control named "file" and it does not have any property "PostedFiles." I double checked :(
I have tested the code before posting and it is working fine.Which version of ASP.NET you are using ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.