Problem
When we Click on a Upload File Button, it should upload the file to the physical directory and then at the same time that file info should be available in a Grid and also from grid itself we should be able to download that file.
Solution
Step 1 - Create an ASP.NET Web application and Add a Webform to it and a folder to save the uploaded file.
Step 2 - Copy the Below HTML Code which is Well Commented to Understand.
- <!DOCTYPE html>
-
- <html>
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <%--ASP.NET control to upload a file--%>
- <asp:FileUpload ID="FileUpload1" runat="server" />
-
- <%--Button to Upload the file--%>
- <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Upload File" />
- <br />
- <br />
- <br />
- <%--Gridview to Display the Available data with file Details--%>
- <%--Generate the OnRowCommand Event Handler of the Gridview Control--%>
- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand="GridView1_RowCommand" Width="513px">
- <AlternatingRowStyle BackColor="White" />
- <Columns>
- <%--Item Template is used to Add a custom link button whose Eval Binding is "File"--%>
- <asp:TemplateField HeaderText="File Name">
- <ItemTemplate>
- <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("File") %>' Text='<%# Eval("File") %>'></asp:LinkButton>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:BoundField DataField="Size" HeaderText="Size (KB)" />
- <asp:BoundField DataField="Type" HeaderText="File Type with Extension" />
- </Columns>
- <EditRowStyle BackColor="#2461BF" />
- <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
- <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
- <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
- <RowStyle BackColor="#EFF3FB" />
- <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
- <SortedAscendingCellStyle BackColor="#F5F7FB" />
- <SortedAscendingHeaderStyle BackColor="#6D95E1" />
- <SortedDescendingCellStyle BackColor="#E9EBEF" />
- <SortedDescendingHeaderStyle BackColor="#4870BE" />
- </asp:GridView>
- </div>
- </form>
- </body>
- </html>
Step 3 - Copy the Below C# Code to the Code behind file which is Well commented to Understand.
- using System;
- using System.Data;
- using System.IO;
- using System.Web.UI.WebControls;
- namespace FileUploadAndDownloadDemo
- {
- public partial class Index : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- ListOfData();
- }
- }
- protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
- {
- Response.Clear();
- Response.ContentType = "application/octet-stream";
- Response.AppendHeader("Content-Disposition", "filename=" + e.CommandArgument);
- Response.TransmitFile(Server.MapPath("~/MyUploads/") + e.CommandArgument);
- Response.End();
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- if (FileUpload1.HasFile)
- {
- string FileName = FileUpload1.FileName;
- FileUpload1.PostedFile.SaveAs(Server.MapPath("~/MyUploads/") + FileName);
- }
- ListOfData();
- }
-
- private void ListOfData()
- {
- DataTable dt = new DataTable();
-
- dt.Columns.Add("File");
- dt.Columns.Add("Size");
- dt.Columns.Add("Type");
-
- foreach (string str in Directory.GetFiles(Server.MapPath("~/MyUploads")))
- {
- FileInfo fileinfo = new FileInfo(str);
- string filename = fileinfo.Name;
- string filesize = (fileinfo.Length / 1024).ToString();
- string filetype = GetFileTypeByFileExtension(fileinfo.Extension);
- dt.Rows.Add(filename, filesize, filetype);
- }
- GridView1.DataSource = dt;
- GridView1.DataBind();
- }
-
- private string GetFileTypeByFileExtension(string fileExtension)
- {
- switch (fileExtension.ToLower())
- {
- case ".doc":
- case ".docx":
- return "Microsoft Word Document";
- case ".xls":
- case ".xlsx":
- return "Microsoft Excel Document";
- case ".txt":
- return "Text File";
- case ".png":
- case ".jpg":
- return "Windows Image file";
- default:
- return "Unknown file type";
- }
- }
-
- }
- }