Upload and save mulitiple image in Database using C# in ASP.Net

Last Reply on Oct 11, 2017 05:55 AM By AnandM

Posted on Oct 11, 2017 05:33 AM

I want to upload the multiple image files with the format IMG_TodayDate_1 like that. When I insert the file IMG_TodayDate_1 image is occured in the database but the IMG_TodayDate_2 is save in the database like 000000000000000000000 and when i view the image the image is not occur.

  protected void btnSumbit_click(object sender, EventArgs e)
        {
            foreach (HttpPostedFile postedFile in fileUpload1.PostedFiles)
            {
                string ext = System.IO.Path.GetExtension(this.fileUpload1.PostedFile.FileName);

                //Change the extension to Capital Letter

                if (ext.ToUpper().Trim() != ".JPG" && ext.ToUpper() != ".PNG" && ext.ToUpper() != ".GIF" && ext.ToUpper() != ".JPEG" && ext.ToUpper() != ".TIF")
                {
                    Response.Write("<script Language='Javascript'> alert ('Only image file allowed')</script>");
                }
                else
                {

                    string filename = Path.GetFileName(postedFile.FileName);
                    string contentType = postedFile.ContentType;
                    using (Stream fs = postedFile.InputStream)
                    {
                        //var filenumber = int.Parse(Request.Files.Count);
                           for (int i = 1; i <= Request.Files.Count; i++)
                        {
                            using (BinaryReader br = new BinaryReader(fs))
                            {
                                byte[] bytes = br.ReadBytes((Int32)fs.Length);
                                string constr = WebConfigurationManager.ConnectionStrings["SqlDbConnection"].ConnectionString;
                                using (SqlConnection con = new SqlConnection(constr))
                                {
                                    string query = "insert into ImageUpload values (@File_Name, @Image_File, @File_Type)";
                                    using (SqlCommand cmd = new SqlCommand(query))
                                    {
                                        cmd.Connection = con;
                                        var name = "IMG" + "_" + System.DateTime.Now + "_" + i;
                                        cmd.Parameters.AddWithValue("@File_Name", name);
                                        cmd.Parameters.AddWithValue("@File_Type", contentType);
                                        cmd.Parameters.AddWithValue("@Image_File", bytes);
                                        con.Open();
                                        cmd.ExecuteNonQuery();
                                        con.Close();
                                    }
                                    Response.Write("<script Language='Javascript'> alert ('The Image File is Save Successfullly.')</script>");
                                    BindData();
                                }
                            }

                        }
                    }

                }
            }
        }

This is the front-end code

<body>
<div align="center">
       <h1>File Upload</h1>
            <b>Select the file to upload:</b><br/>
        <asp:Label ID="lblFileID" runat="server" Text="Label" Visible="false"></asp:Label>
            <br/>
        <asp:Button ID="btnSubmit" runat="server" Text="Save" OnClick="btnSumbit_click" />
        <asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_click"/>
<br/>
</body>

 

You are viewing reply posted by: AnandM on Oct 11, 2017 05:55 AM.
Posted on Oct 11, 2017 05:55 AM Modified on on Oct 11, 2017 05:56 AM
SamMyat says:
protected void btnSumbit_click(object sender, EventArgs e)
      {
          foreach (HttpPostedFile postedFile in fileUpload1.PostedFiles)
          {
              string ext = System.IO.Path.GetExtension(this.fileUpload1.PostedFile.FileName);
 
              //Change the extension to Capital Letter
 
              if (ext.ToUpper().Trim() != ".JPG" && ext.ToUpper() != ".PNG" && ext.ToUpper() != ".GIF" && ext.ToUpper() != ".JPEG" && ext.ToUpper() != ".TIF")
              {
                  Response.Write("<script Language='Javascript'> alert ('Only image file allowed')</script>");
              }
              else
              {
 
                  string filename = Path.GetFileName(postedFile.FileName);
                  string contentType = postedFile.ContentType;
                  using (Stream fs = postedFile.InputStream)
                  {
                      //var filenumber = int.Parse(Request.Files.Count);
                         for (int i = 1; i <= Request.Files.Count; i++)
                      {
                          using (BinaryReader br = new BinaryReader(fs))
                          {
                              byte[] bytes = br.ReadBytes((Int32)fs.Length);
                              string constr = WebConfigurationManager.ConnectionStrings["SqlDbConnection"].ConnectionString;
                              using (SqlConnection con = new SqlConnection(constr))
                              {
                                  string query = "insert into ImageUpload values (@File_Name, @Image_File, @File_Type)";
                                  using (SqlCommand cmd = new SqlCommand(query))
                                  {
                                      cmd.Connection = con;
                                      var name = "IMG" + "_" + System.DateTime.Now + "_" + i;
                                      cmd.Parameters.AddWithValue("@File_Name", name);
                                      cmd.Parameters.AddWithValue("@File_Type", contentType);
                                      cmd.Parameters.AddWithValue("@Image_File", bytes);
                                      con.Open();
                                      cmd.ExecuteNonQuery();
                                      con.Close();
                                  }
                                  Response.Write("<script Language='Javascript'> alert ('The Image File is Save Successfullly.')</script>");
                                  BindData();
                              }
                          }
 
                      }
                  }
 
              }
          }
      }

 replace above code with below.

protected void btnSumbit_click(object sender, EventArgs e)
{
    int i = 1;
    foreach (HttpPostedFile postedFile in fileUpload1.PostedFiles)
    {     
        string ext = System.IO.Path.GetExtension(this.fileUpload1.PostedFile.FileName);

        if (ext.ToUpper().Trim() != ".JPG" && ext.ToUpper() != ".PNG" && ext.ToUpper() != ".GIF" && ext.ToUpper() != ".JPEG" && ext.ToUpper() != ".TIF")
        {
            Response.Write("<script Language='Javascript'> alert ('Only image file allowed')</script>");
            return;
        }
        else
        {
            string filename = Path.GetFileName(postedFile.FileName);
            string contentType = postedFile.ContentType;
            using (Stream fs = postedFile.InputStream)
            {
                using (BinaryReader br = new BinaryReader(fs))
                {
                    byte[] bytes = br.ReadBytes((Int32)fs.Length);
                    string constr = WebConfigurationManager.ConnectionStrings["SqlDbConnection"].ConnectionString;
                    using (SqlConnection con = new SqlConnection(constr))
                    {
                        string query = "insert into ImageUpload values (@File_Name, @Image_File, @File_Type)";
                        using (SqlCommand cmd = new SqlCommand(query))
                        {
                            cmd.Connection = con;
                            var name = "IMG" + "_" + System.DateTime.Now + "_" + i;
                            cmd.Parameters.AddWithValue("@File_Name", name);
                            cmd.Parameters.AddWithValue("@File_Type", contentType);
                            cmd.Parameters.AddWithValue("@Image_File", bytes);
                            con.Open();
                            cmd.ExecuteNonQuery();
                            con.Close();
                        }
                        i++;
                    }
                }
            }
        }
    }
    BindData();
}

For more details refer below articles.

Upload and save (insert) multiple files to database in ASP.Net using C# and VB.Net

Upload multiple files with ASP.Net 4.5 FileUpload control in Visual Studio 2012 and 2013

Uploading Multiple Files using FileUpload Control in ASP.Net 2.0, 3.0 and 3.5