Thursday, 24 January 2013

code for upload image using file upload control in asp.net c#

code for upload image using  file upload control  in asp.net c#



 protected void griddata()
    {
        string qry = "select imageid,imagename,imagepath from fileupload";
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        NpgsqlDataAdapter da = new NpgsqlDataAdapter(qry, con);
        DataSet   ds = new DataSet();
        da.Fill(ds);
        gdvfileupload.DataSource = ds;
        gdvfileupload.DataBind();
        con.Close();
    }
 
    protected void btnSaveImage_Click1(object sender, EventArgs e)
    {
       
        if (FileuploadImg.PostedFile != null)
        {
            string fileExt = System.IO.Path.GetExtension(FileuploadImg.FileName);

            if (fileExt == ".jpeg" || fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".png")
            {
                string imgName = FileuploadImg.FileName;
                string imgPath = FileuploadImg.PostedFile.FileName;

                FileuploadImg.SaveAs(Server.MapPath("images/" + imgName));

                filePath = Server.MapPath("~/Files/images/" + imgName);
                string filename1 = Path.GetFileName(filePath);
                //filename1 = Path.GetFileNameWithoutExtension(myFile);
                string str = "insert into fileupload(imagename,imagepath) values('" + filename1 + "','" + imgPath + "')";
                con.Open();
                NpgsqlCommand cmd = new NpgsqlCommand(str, con);
                cmd.ExecuteNonQuery();
                con.Close();
                Response.Write("<script>alert('Uploaded Successfully....')</script>");
                griddata();
               
            }
            else               
            {
                Response.Write("<script>alert('Invalid file....')</script>");
            }
        }
    }

No comments:

Post a Comment