welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Monday, June 11, 2012

Upload and Download file from a database in

In a previous post i posted about how to validate the upload images and how to upload in db.

Here its special posts for complete tutorial for upload and download a file from database.
Let us see details about the article here i added file upload control and grid view..
Initially see the database design view
Create table with the following field
create table FILEDB (ID int identity(1,1),FLNAME varchar(25),FLTYPE varchar(25),FLDATA image)

FLNAME=Used to store the filename.
FLTYPE=Used to store the file type extension
FLDATA=Used to store bytes value.
Here i share only the important code used to upload and download any file from database.
Design View:
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />
        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" DataKeyNames="ID"
                <asp:BoundField HeaderText="FileName" DataField="FLNAME" />
                <asp:BoundField HeaderText="File Type" DataField="FLTYPE" />
                <asp:TemplateField HeaderText="Fileupload">
                        <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%#Eval("ID")%>'

1. choose the upload file using file upload control
2. click the submit button to upload in to DB
3. Finally load the uploaded file into gridview
4. Click the Download button, download the uploaded file from database.
Code for submit button event:
protected void Button1_Click(object sender, EventArgs e)
        string fname, ftype;
        byte[] rdbyte = null;
        BinaryReader br;
        Stream fs;
        if (FileUpload1 .HasFile) {
            fname = FileUpload1.FileName;
            ftype = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".") + 1);
            fs = FileUpload1.PostedFile.InputStream;
            br = new BinaryReader(fs);
            rdbyte  = br.ReadBytes(FileUpload1.PostedFile.ContentLength);

            fselect ="insert into FILEDB values('"+ fname +"','" + ftype +"',@imagerd)";

            sqlcmd = new SqlCommand(fselect, sqlcon);
            sqlcmd.CommandType = CommandType.Text;
            sqlcmd.Parameters.AddWithValue("@imagerd", rdbyte);

Below code for grid view link button row command event to download the file..

protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
        if (e.CommandName == "Download")
            fselect = "select FLDATA,FLTYPE,FLNAME from FILEDB where ID='" + e.CommandArgument + "'";
            sqlcmd = new SqlCommand(fselect, sqlcon);
            sqldr = sqlcmd.ExecuteReader();
            if (sqldr.HasRows)
                Response.ContentType = sqldr["FLTYPE"].ToString();
                Response.AppendHeader("content-Disposition", "attachment;filename=" + sqldr["FLNAME"].ToString() +"");

If you like this article share the post with your friends..
Happy coding


Post a Comment