0

I have problem with my file uploading. If I select multiple files, only a single file gets stored in the folder. Suppose I have selected 3 different files in one - then only the first file gets stored three times.

private void galleryImagesFunction()
{
    try {
        foreach (HttpPostedFile postedfile in galleryUpload.PostedFiles) {
            System.IO.FileInfo file = new System.IO.FileInfo(galleryUpload.PostedFile.FileName);
            string newname = file.Name.Remove((file.Name.Length - file.Extension.Length));
            newname = (prodID.Text + "_" + newname + System.DateTime.Now.ToString("_ddMMyyhhmmss")) + file.Extension;
            postedfile.SaveAs(Server.MapPath("/images/products/") + newname);
            string filepath = Convert.ToString("/images/products/") + newname;

            MySqlConnection con2 = new MySqlConnection();
            con2.ConnectionString = ConfigurationManager.ConnectionStrings("conio").ConnectionString();
            string query = "INSERT into table (`image`, `productID`, `status`) values (@image, @productID, @status)";
            MySqlCommand cmd2 = new MySqlCommand(query, con2);
            con2.Open();
            cmd2.Parameters.AddWithValue("@image", filepath);
            cmd2.Parameters.AddWithValue("@productID", prodID.Text);
            cmd2.Parameters.AddWithValue("@status", "active");
            cmd2.ExecuteNonQuery();
            con2.Close();
        }
    } catch (Exception ex) {
        Response.Write(ex);
    }
}

UPDATE (Form Code)

<asp:FileUpload ID="galleryUpload" runat="server" ClientIDMode="Static" CssClass="hiddenl" AllowMultiple="true" />

UPDATE (Web.config)

<system.web>
      <compilation debug="true" strict="false" explicit="true" targetFramework="4.5.2" />
      <httpRuntime requestPathInvalidCharacters="" requestValidationMode="2.0"/>

    <customErrors mode="Off"/>
    <trust level="Full"/>
      <pages validateRequest="false"></pages>
</system.web>
SUN
  • 973
  • 14
  • 38
  • Can you include your form code? – m.qayyum Aug 13 '16 at 14:46
  • @m.qayyum update my post – SUN Aug 13 '16 at 14:48
  • AllowMultiple="true" is only supported in .NET 4.5 or higher http://stackoverflow.com/questions/17441925/how-to-choose-multiple-files-using-file-upload-control You are using correct framework? – m.qayyum Aug 13 '16 at 14:53
  • @m.qayyum I am using 4.5.2 . I have added web.cofig code please check once. Also I have noticed in my folder Image got stored once only & only one image save out of 3. And in my db table it got entered thrice which resulting it shows me one image three times – SUN Aug 13 '16 at 14:59
  • 1
    galleryUpload.PostedFile.FileName this should be postedfile.FileName – m.qayyum Aug 13 '16 at 15:05

0 Answers0