Click here to Skip to main content
15,897,094 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
any simple idea to upload and retrive images from database? i have a database profile 2 data filed imagename and imagepath
Posted
Updated 2-Feb-18 20:31pm

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace fileupld
{
    public partial class _Default : System.Web.UI.Page
    {
     
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

      

        protected void onbtnupldclick(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {

                string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);

                if (fileExtension.ToLower() != ".jpg" && fileExtension.ToUpper() != ".jpeg")
                {
                    Response.Write("<script>alert('Please Upload .jpeg Or .jpg Files only');</script>");

                }
                else
                {

                    int fileSize = FileUpload1.PostedFile.ContentLength;

                    if (fileSize >= 1000000)
                    {
                        Response.Write("<script>alert('Please Upload less of 1 mb');</script>");
                    }
                    else
                    {
                        con.Open();
                        FileUpload1.SaveAs(Server.MapPath("~/folder/" + FileUpload1.FileName));
                        String s1 = FileUpload1.FileName;
                        String s2 = "insert into fileupload values('" + s1 + "')";
                        SqlCommand cmd = new SqlCommand(s2, con);
                        cmd.ExecuteNonQuery();
                        Response.Write("<script>alert('File Uploaded');</script>");
                    }
                }
            }
            else
            {

                Response.Write("<script>alert('Please Select The File');</script>");
            } 
        }

        protected void btnviewclick(object sender, EventArgs e)
        {
            con.Open();
            string sql = "select filename from fileupload";
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {

                //string filePath = "Images\\" + dt.Rows[0][1].ToString(); 
                //Response.ContentType = "Images/jpg";
                //Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");
                //Response.TransmitFile(Server.MapPath(filePath));
                //Response.End();  
               
                //Response.AppendHeader("Content-Disposition", "attachment; filename=~/folder/"+ dt.Rows[0][0].ToString());
                
                Response.Write("<script>window.open('../Picture/"+dt.Rows[0][0].ToString()+"','-blank');</script>");
              
                
               
            }

           
                
            
        }
    }
}
 
Share this answer
 
Comments
CHill60 3-Feb-18 7:36am    
This is just a code dump not a solution to the 3+ year old question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900