duck
duck

Reputation: 867

FileUpload uploads 1 file multiple times

For some reason when I try to upload multiple files all the files all become copies of the first one. When I say copies I mean different file names, different file extensions, but the same picture across all images.

Example:

I select four files. 2 png's, 2 jpg's. All different sizes. When I upload them, they come out like this:

I am very confused as to while this is happening. Response.Write shows the files as being different sizes but they're saved as the same file.

 foreach (HttpPostedFile file in FileUpload1.PostedFiles)
 {
    // Debugging only - I know Response.Write is bad to use
    Response.Write(file.FileName + " - " + file.ContentLength + "<br>");

    // Normally a server, set to C for debugging
    FileUpload1.SaveAs(Path.Combine("C:\\Uploads"), file.FileName);
 }

Output:

 1.png - 270587
 2.png - 261286
 3.JPG - 1309586
 4.JPG - 912675

So it knows they are different files but it does one file four times. Any idea why?

Upvotes: 3

Views: 1594

Answers (3)

Rhys
Rhys

Reputation: 2173

FileUpload1.SaveAs(Path.Combine("C:\\Uploads"), file.FileName);

won't work. You need to operate on file object which traverses through the FileUpload1.PostedFiles list.

Use file.SaveAs(...) instead.

Take a look at: http://msdn.microsoft.com/en-us/library/system.web.httprequest.files(v=vs.110).aspx for an example / more information.

Upvotes: 1

LazyOfT
LazyOfT

Reputation: 1438

According to MSDN if you are handling multiple files you should use the Request.Files collection in order to iterate them. Try something like this:

 foreach (HttpPostedFile file in Request.Files)
 {
    // Debugging only - I know Response.Write is bad to use
    Response.Write(file.FileName + " - " + file.ContentLength + "<br>");

    // Normally a server, set to C for debugging
    file.SaveAs(Path.Combine("C:\\Uploads"), file.FileName);
 }

Upvotes: 2

suff trek
suff trek

Reputation: 39777

I think u meant

file.SaveAs(Path.Combine("C:\\Uploads"), file.FileName)

and not

FileUpload1.SaveAs(Path.Combine("C:\\Uploads"), file.FileName)

Upvotes: 1

Related Questions