upload and download file

Keywords | upload and download file asp.net, upload and download file in asp.net c#, file upload and download from database in asp.net, file upload and download example

Upload and Download File C#

Uploading and downloading files are the most common methods for any website or asp.net web application. The FileUpload web server control allows you to provide users with a way to send a file from their computer to the server in asp.net.

Note: The maximum size file that can be uploaded depends on the value of the MaxRequestLength configuration setting. If users attempt to upload a file that is larger than the maximum, the upload fails.

Upload and Download File in ASP.Net C#

In this article, I will demonstrates how to upload and download files from folder in asp.net using c#. The following file upload and download example shows how to work with an uploaded file. The code checks the file name extension of the uploaded file against a hard-coded list of allowed file name extensions and rejects all other types of files. Find the dot net source code below :-

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Save/Upload files in folder and download files from folder system.</title>
    <form id="form1" runat="server">
        <h2>Upload and Download file in asp.net using c#</h2>
            <table style="padding: 20px; font-family: Verdana, Geneva, Tahoma, sans-serif; font-size: medium; border: 1px solid black;" width="60%" cellpadding="25">
                        <asp:Label ID="lblFilename" runat="server" Text="Browse:"></asp:Label>
                        <asp:FileUpload ID="fileUpload1" runat="server" />
                        <asp:LinkButton ID="OnLnkUpload" runat="server" OnClick="OnLnkUpload_Click"
                            Font-Underline="False" Font-Size="11pt">Upload</asp:LinkButton></td>
                        <asp:LinkButton ID="OnLnkDownload" runat="server" OnClick="OnLnkDownload_Click"
                            Font-Underline="False" Font-Size="11pt">Download</asp:LinkButton></td>
                    <td colspan="4">
                        <asp:Label ID="lblMessage" runat="server" ForeColor="#009900"></asp:Label></td>

using System;
using System.IO;

public partial class _Default : System.Web.UI.Page
    string filename = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    { }

    //To upload file 
    protected void OnLnkUpload_Click(object sender, EventArgs e)
        filename = Path.GetFileName(fileUpload1.PostedFile.FileName);
        fileUpload1.SaveAs(Server.MapPath("Files/" + filename));
        lblFilename.Text = "Files/" + fileUpload1.FileName;
        lblMessage.Text = "File uploaded sucessfully to the folder: -" + " Files/" + fileUpload1.FileName;

    // To download uplaoded file
    protected void OnLnkDownload_Click(object sender, EventArgs e)
        if (lblFilename.Text != string.Empty)
            if (lblFilename.Text.EndsWith(".txt"))
                Response.ContentType = "application/txt";
            else if (lblFilename.Text.EndsWith(".pdf"))
                Response.ContentType = "application/pdf";
            else if (lblFilename.Text.EndsWith(".docx"))
                Response.ContentType = "application/docx";
                Response.ContentType = "image/jpg";

            string filePath = lblFilename.Text;

            Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");


Download Code

What do you think?

I hope you liked this article. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.


Please enter your comment!
Please enter your name here