Upload and save multiple images in folder using C# in ASP.Net

Last Reply 3 months ago By kalpesh

Posted 3 months ago

getting difficulty to store the image in the give path and because the image is not store in the database it not shown in the web page

 <asp:FileUpload ID="fuMultipleImage" runat="server"  AllowMultiple="true"  />
    public void InsertMultipleImage()
    {
        BindDetails();

        foreach (HttpPostedFile postedFile in fuMultipleImage.PostedFiles)
        {

            string filename = Path.GetFileName(postedFile.FileName);
            string targetPath = Server.MapPath("~/Co-Working/Images/" + filename);
            Stream strm = fuMultipleImage.PostedFile.InputStream;
            var targetFile = targetPath;

         
		 
            fuMultipleImage.SaveAs(Server.MapPath("~/Co-Working/Images/" + targetPath));

        string constr = ConfigurationManager.ConnectionStrings["IECNEWConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {

            string query = "insert into tblFiles values (@Name, @ContentType, @Data, @CoworkingId)";
            using (SqlCommand cmd = new SqlCommand(query))
            {
                cmd.Connection = con;
                cmd.Parameters.AddWithValue("@Name", filename);
                cmd.Parameters.AddWithValue("@ContentType", "~/Co-Working/Images/" + filename);
                cmd.Parameters.AddWithValue("@Data", 0);
                cmd.Parameters.AddWithValue("@CoworkingId", ViewState["ID"]);

                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }

            }

        }
    }




 

1 property-detail-02.jpg Images\p\property-detail-02.jpg 0x00000000

12 property-detail-03 (1).jpg Images\p\property-detail-03 (1).jpg 0x00000000

13 property-detail-02.jpg F:\Projects\India Entrepreneur Club\Backup\30-09-2017\IEC\Co-Working\Images\p\property-detail-02.jpg 0x00000000

34 property-detail-03 (1).jpg F:\Projects\India Entrepreneur Club\Backup\30-09-2017\IEC\Co-Working\Images\p\property-detail-03 (1).jpg 0x00000000

35 property-detail-02.jpg F:\Projects\India Entrepreneur Club\Backup\30-09-2017\IEC\Co-Working\Images\property-detail-02.jpg 0x00000000

46 property-detail-03.jpg F:\Projects\India Entrepreneur Club\Backup\30-09-2017\IEC\Co-Working\Images\property-detail-03.jpg 0x00000000

47 Deepa kar.jpg F:\Projects\India Entrepreneur Club\Backup\30-09-2017\IEC\Co-Working\Images\p\Deepa kar.jpg 0x00000000

58 HUsain-Bolt.jpg F:\Projects\India Entrepreneur Club\Backup\30-09-2017\IEC\Co-Working\Images\p\HUsain-Bolt.jpg 0x00000000

59 sakshi.jpg F:\Projects\India Entrepreneur Club\Backup\30-09-2017\IEC\Co-Working\Images\p\sakshi.jpg 0x00000000

510 Sitves sindhu.jpg F:\Projects\India Entrepreneur Club\Backup\30-09-2017\IEC\Co-Working\Images\p\Sitves sindhu.jpg 0x00000000

511 IMG_20170910_105600-min.jpg Images/p/IMG_20170910_105600-min.jpg 0x00000000

712 IMG_20170909_223610-min.jpg Images/p/IMG_20170909_223610-min.jpg 0x00000000

813 1.jpg Images/p/1.jpg 0x00000000

914 1.jpg Images/p/1.jpg 0x00000000

915 a.jpg Images/p/a.jpg 0x00000000

1116 a.jpg Images/p/a.jpg 0x00000000

Posted 3 months ago

You are doing totally wrong. If you need to save image in folder path then don’t need to pass Data and content Type. Just need to save the path in database and image in folder path.

If you want to store the Image in folder and path in database then you just need to save path in database column where you are storing image path.

First be clear you need to store either images in Database only then Given article link in first reply is ok for you if you need to store the Image in Folder and path in database then you need to modify your code which is more confusing for saving logic which you have implemented.

You need to ignore data and ContentType column from table and need to add path as column in table.

C#

Refere the Below link where single file uplaod code is given.

 protected void btnUpload_Click(object sender, EventArgs e)
    {
        foreach (HttpPostedFile postedFile in FileUpload1.PostedFiles)
        {
            string filename = Path.GetFileName(postedFile.FileName);

            //Add Entry to DataBase
            String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
            SqlConnection con = new SqlConnection(strConnString);
            string strQuery = "insert into tblFilesPath ([Name] ,[Path],[CoworkingId]) values(@FileName, @FilePath,@CoworkingId)";
            SqlCommand cmd = new SqlCommand(strQuery);
            cmd.Parameters.AddWithValue("@FileName", FileName);
            cmd.Parameters.AddWithValue("@FilePath", "Co-Working/Images/" + FileName);
            cmd.Parameters.AddWithValue("@CoworkingId", ViewState["ID"]);
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();

                //Save files to disk
                fuMultipleImage.SaveAs(Server.MapPath("Co-Working/Images/" + FileName));
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }
        BindDetails();
    }
I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html