Wednesday, October 8, 2014

How to save the Image to folder from data base in asp.net

How to save the Image to folder from data base in asp.net

1)  By using this method getting the image bytes and save to the folder

public void SaveImageToFolder()
        {
            try
            {
                DataTable dt = new DataTable();
                byte[] byts = null;
                string strcon = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;
                SqlConnection connection = new SqlConnection(strcon);
                connection.Open();
                SqlCommand command = new SqlCommand("select COMPANY_LOGO from COMPANYIMAGES where COMPANY_SYSID='" + GPRSTrackSession.CompanyCode + "'", connection);
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    Byte[] bytes = (Byte[])dt.Rows[0]["COMPANY_LOGO"];

                    var pathName = "~/Images/rptimgs.jpg";
                    var fileName = Server.MapPath(pathName);

                    FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
                    fileStream.Write(bytes, 0, bytes.Length);
                    fileStream.Close();
                }
            }
            catch (Exception ex)
            {

            }

        }

2) And assign that path to the image url in the application
string imgPath = Request.Url.AbsoluteUri.Split('/')[0].ToString() + "//" + Request.Url.AbsoluteUri.Split('/')[2] + "/images/rptimgs.jpg";
                            imgReportHeading.ImageUrl = imgPath;




No comments:

Post a Comment