Question

When I am trying to upload pics, same pic is getting uploaded with different name

   HttpFileCollection uploadedFiles = Request.Files;
            for (int i = 1; i < uploadedFiles.Count; i++)
            {
                HttpPostedFile userPostedFile = uploadedFiles[i];

                string filename = userPostedFile.FileName;
                string filepath = Path.GetFileName(filename);
                FileUpload1.SaveAs(Server.MapPath("~/Uploads/") + filepath);

}

Lets say I am upload images a.jpeg, b.jpeg and c.jpeg then in my upload folder I am seeing a.jpeg, b.jpeg and c.jpeg but they all look like a.jpeg

EDIT1 ASPX code

    <asp:UpdatePanel ID="UpdatePanel2" UpdateMode="Conditional" runat="server">
      <ContentTemplate>
           <asp:FileUpload  AllowMultiple="true" ID="FileUpload1" runat="server" />
          ......

 </asp:UpdatePanel>
Was it helpful?

Solution

I think this line...

FileUpload1.SaveAs(Server.MapPath("~/Uploads/") + filepath);

Should be...

userPostedFile.SaveAs(Server.MapPath("~/Uploads/") + filepath);

OTHER TIPS

That should be due to your html.

Look at your input html. Like below.

<input type="file" name="file1" id="file1" /> 
<input type="file" name="file2" id="file2" />
....

or file array

<input type="file" name="file[1]" id="file1" />
<input type="file" name="file[2]" id="file1" />
...

In your code, your input names are probably same. That is why your are getting the same file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top