vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Monday, June 11, 2012

Upload and Download file from a database in asp.net


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:
<div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />
    </div>
    <div>
        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" DataKeyNames="ID"
            OnRowCommand="GridView2_RowCommand">
            <Columns>
                <asp:BoundField HeaderText="FileName" DataField="FLNAME" />
                <asp:BoundField HeaderText="File Type" DataField="FLTYPE" />
                <asp:TemplateField HeaderText="Fileupload">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%#Eval("ID")%>'
                            CommandName="Download">Download</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>


Process:
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;
//********www.dotnetcode.in*********
            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);
            sqlcon.Open();
            sqlcmd.CommandType = CommandType.Text;
            sqlcmd.Parameters.AddWithValue("@imagerd", rdbyte);
            sqlcmd.ExecuteNonQuery();
            sqlcon.Close();
        }
    }

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

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


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

0 comments:

Post a Comment