Search code examples
c#imageasp.net-4.5httppostedfile

Image content is same for different images uploaded


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>

Solution

  • I think this line...

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

    Should be...

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