Wednesday, September 23, 2015

How to upload and download files

Suggested Videos
Part 136 - Provide capability to start and stop image slideshow
Part 137 - Add images to slideshow using xml file
Part 138 - Add images to slideshow using database table

In this video, we will discuss
1. Uploading files
2. Displaying the list of files that are already uploaded
3. Downloading files

The user interface to upload and download files, should be as shown below. 

When the files are uploaded, they should be uploaded to a folder on the web server. In our case, we will be uploading to "Data" folder.

WebForm1.aspx code:
<div style="font-family:Arial">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Upload" 
    OnClick="Button1_Click" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    OnRowCommand="GridView1_RowCommand" BackColor="White" 
    BorderColor="#CC9966" BorderStyle="None" 
    BorderWidth="1px" CellPadding="4">
        <asp:TemplateField HeaderText="File" ShowHeader="False">
                <asp:LinkButton ID="LinkButton1" runat="server" 
                    CommandArgument='<%# Eval("File") %>'
                    CommandName="Download" Text='<%# Eval("File") %>'>
        <asp:BoundField DataField="Size" HeaderText="Size in Bytes" />
        <asp:BoundField DataField="Type" HeaderText="File Type" />
    <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
    <HeaderStyle BackColor="#990000" Font-Bold="True" 
        ForeColor="#FFFFCC" />
    <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" 
        HorizontalAlign="Center" />
    <RowStyle BackColor="White" ForeColor="#330099" />
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" 
        ForeColor="#663399" />
    <SortedAscendingCellStyle BackColor="#FEFCEB" />
    <SortedAscendingHeaderStyle BackColor="#AF0101" />
    <SortedDescendingCellStyle BackColor="#F6F0C0" />
    <SortedDescendingHeaderStyle BackColor="#7E0000" />

WebForm1.aspx.cs code:
protected void Button1_Click(object sender, EventArgs e)
    if (FileUpload1.HasFile)
        string fileName = FileUpload1.FileName;
            .SaveAs(Server.MapPath("~/Data/") + fileName);

    DataTable dt = new DataTable();

    foreach (string strfile in Directory.GetFiles(Server.MapPath("~/Data")))
        FileInfo fi = new FileInfo(strfile);
        dt.Rows.Add(fi.Name, fi.Length.ToString(), 

    GridView1.DataSource = dt;

private string GetFileTypeByExtension(string fileExtension)
    switch (fileExtension.ToLower())
        case ".docx":
        case ".doc":
            return "Microsoft Word Document";
        case ".xlsx":
        case ".xls":
            return "Microsoft Excel Document";
        case ".txt":
            return "Text Document";
        case ".jpg":
        case ".png":
            return "Image";
            return "Unknown";

protected void GridView1_RowCommand(object sender, 
    GridViewCommandEventArgs e)
    Response.ContentType = "application/octet-stream";
    Response.AppendHeader("Content-Disposition", "filename=" 
        + e.CommandArgument);
        + e.CommandArgument);

Please make sure to include the following using declarations in the code behind file.
using System.IO;
using System.Data;

If you are searching life partner. your searching end with now offer free matrimonial website which offer free message, free chat, free view contact information. so register here : Free matrimonial website