Tuesday, 24 December 2013

File Upload and Download in ASP.NET

In This article we are going to see how to create a application to upload a file and list the content as gridview and while click the name of file in gridview download the file to the user.

File Upload
  Make a File upload control and save the file in server under files folder, Make an entry in gridview

  <form id="form1" runat="server">
    <div align="center">
    <br />
        <asp:FileUpload ID="fileupload1" runat="server" />
        <asp:Button ID="upload" Text="Upload" runat="server" onclick="upload_Click" />
    <br />
    <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            CellPadding="4" EnableModelValidation="True" ForeColor="#333333"
            GridLines="None" onrowcommand="GridView1_RowCommand">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <asp:TemplateField HeaderText="Name">
                        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="download"
                            CommandArgument='<%# Eval("Name") %>' Text='<%# Eval("Name") %>'></asp:LinkButton>
                <asp:BoundField DataField="Type" HeaderText="Type" />
                <asp:BoundField DataField="Size" HeaderText="Size in Bytes" />
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />

DataTable dt = new DataTable();

        protected void Page_Load(object sender, EventArgs e)

        protected void upload_Click(object sender, EventArgs e)
            if (fileupload1.HasFile)
                fileupload1.PostedFile.SaveAs(Server.MapPath("~/Files/" + fileupload1.FileName));

            foreach(string filename in Directory.GetFiles(Server.MapPath("~/Files/")))
                FileInfo fi=new FileInfo(filename);  
                DataRow row = dt.NewRow();
                row["Name"] = fi.Name;
                row["Size"] =fi.Length;
                row["Type"] = fi.Extension;

            GridView1.DataSource = dt;


 For download we have to change the response of the click event in rowcommand event of gridview.


        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
            if (e.CommandName == "download")
                Response.ContentType = "Application/octect-stream";
                Response.AddHeader("content-disposition", "filename=" + e.CommandArgument);
                Response.TransmitFile("~/Files/" + e.CommandArgument);

This article will help you the guys who want to create a code for upload and download the file.

No comments:

Post a Comment