HOW TO INSERT AND DOWNLOAD THE DOCUMENTS FROM DATABASE

         

Create Images table in required database like show in Image1

Image1
For insert document into the database:
Aspx page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Insert Documents</title>     
</head>
<body>
    <form id="form1" runat="server">
    <div id="div1" runat="server">
   
    <asp:Label ID="lbldocument" Text="Document" runat="server"></asp:Label>
    <asp:FileUpload ID="Fileupload1" runat="server" />
    <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Click_btnUpload" />
    </div>
    </form>
</body>
</html>

Aspx.cs :

 public void Click_btnUpload(object sender, EventArgs e)
    {
        if(Fileupload1.HasFile)
        {
            try
            {
                SqlConnection connection = new SqlConnection(@"Data Source=Ravinder-05\SQLEXPRESS;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=Admin123");
                //getting length of uploaded file
                int length = Fileupload1.PostedFile.ContentLength;
                //create a byte array to store the binary image data
                byte[] imgbyte = new byte[length];
                //store the currently selected file in memeory
                HttpPostedFile img = Fileupload1.PostedFile;
                //set the binary data
                img.InputStream.Read(imgbyte, 0, length);
                string fileName = Path.GetFileNameWithoutExtension(Fileupload1.FileName);
                string extension = Path.GetExtension(Fileupload1.FileName);
                connection.Open();
                SqlCommand cmd = new SqlCommand("insert into Images(Name,Document,FileExtenstion)values(@docname,@docdata,@extension)", connection);
                cmd.Parameters.Add("@docname", SqlDbType.VarChar, 50).Value = fileName;
                cmd.Parameters.Add("@docdata", SqlDbType.Image).Value = imgbyte;
                cmd.Parameters.Add("@extension", SqlDbType.VarChar, 50).Value = extension;
                int count = cmd.ExecuteNonQuery();
                connection.Close();
                if (count == 1)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + fileName + " Document inserted successfully')", true);
                }
            }
            catch(Exception e) 
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('Document is not uploaded due to " + e.Message + " ')", true);
            }
        }
    }
After Document Inserted this message show


For Download the Document from database:


Add to above aspx page to below Link button

<asp:LinkButton ID="lnkDownload" runat="server" Text="Document Download" OnClick="Click_lnkDownload"></asp:LinkButton>


Write below code in Link button click event:

 public void Click_lnkDownload(object sender, EventArgs e)
    {
        try
        {
            SqlConnection conn = new SqlConnection(@"Data Source=Ravinder-05\SQLEXPRESS;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=Admin123");
            string qury = "select Name,Document,FileExtenstion from Images where ID=1";
            SqlCommand cmd = new SqlCommand(qury, conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            Byte[] bytes = (Byte[])ds.Tables[0].Rows[0]["Document"];

            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment;filename=" + "" + ds.Tables[0].Rows[0]["Name"].ToString() + "." + ds.Tables[0].Rows[0]["FileExtenstion"].ToString() + "");
            BinaryWriter bw = new BinaryWriter(Response.OutputStream);
            bw.Write(bytes);

            Response.End();
            Response.Flush();
        }
        catch(Exception ex) {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('Document is not Download due to " + ex.Message + " ')", true);
        }
    }


At the time of Download the document






Comments

Popular posts from this blog

Cross browsers detect print events using javascript

Setup and implement simple angular.js program in MVC