Introduction to ASP.NET Download File
ASP.NET provides methods and implicit object Response to download a file from the server. To add a feature of downloading a file from the server to the local machine, we can use these methods in our application. We have many functionalities in ASP.net through which we can create our web page attractive and easy to use it by the end customer. However, have you ever wondered that if anyone wants to upload a file or download a file that contains huge data? Answer to this question is “yes” it is possible via ASP.NET.
string filename = "name of the file from the database";
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
string aaa = Server.MapPath("~/SavedFolder/" + filename);
Response.TransmitFile(Server.MapPath("~/SavedFolder/" + filename));
Steps to Download File in ASP.NET
Now we will see how exactly we can download file.
- Open a Visual Studio and create a blank new web application.
- We want a button “Download” so that after clicking, it will provide us the window pop up for downloading.
- Click on the properties. Drag and drop the button from the toolbox.
- Now change the property of the button to the text to the Download file so as it will display on the button.
- Now we need to set the Content-Type in the code.we can have a look at the given content type used below.
- In code-behind class for the page, collect information about available files to download, by using the GetFiles method of the Directory class.
- Now we need to Process the Download button click event, after that stream the selected file to the browser using the Response object.
- We need to perform a two-step process to download a file from the server:
- Your code has to collect and present to the user a list of the available files, which will be downloaded along with a button to initiate the download.
- Your code has to process the button click event and stream the selected file to the browser.
- Page_Load method of the code-behind contains a list of files, which is located in C:\Downloads directory.
- We can also use a particular specific directory on the webserver instead.
- By using the GetFiles method of the Directory class, ListBox is populated to collect the files in the specified folder.
- When a user wants to download and it clicks on the download button, the btnDownload_Click method in the code-behind executes.
- Now the routine calls another routine BufferedFileDownload.
- The routine BufferedFileDownload will checks if the file exists, which is one of the major tasks is.
- Firstly, we need to call the clear method so that it will remove the data in the buffer stream.
- We will receive a corrupt error file if the response object already has data in it and we try to write a file.
- To add the name of the file which we want to download, we need to use the AddHeader method of the Response Object and its length to the output stream that also before writing the file.
- ContentType method needs to be added to specify the content type of the file.
- If we set the content type to the explicit file type like application/PDF or application/msword will allow the browser to open it with the application on the client machine.
- Below are some examples with the most common content types:
- .htm, .html – Response.ContentType = “text/HTML”;
- .txt – Response.ContentType = “text/plain”;
- .doc, .rtf, .docx – Response.ContentType = “Application/msword”;
- .xls, .xlsx – Response.ContentType = “Application/x-msexcel”;
- .jpg, .jpeg – Response.ContentType = “image/jpeg”;
- .gif – Response.ContentType = “image/GIF”;
- .pdf – Response.ContentType = “application/pdf”;
Implementing ASP.NET Download File
Let us take an example so that it will be more clear about downloading the file in asp.net. Here we will take a simple example in which the abc.text file will be downloaded.
Code for Default.aspx:
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="FileDownloadExample._Default" %>
<form id="form1" runat="server">
<p>Click Download File button to download a file</p>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Download File" />
<asp:Label ID="Label1" runat="server"></asp:Label>
Code for Default.aspx.cs:
public partial class _Default : Page
protected void Page_Load(object sender, EventArgs e)
protected void Button1_Click(object sender, EventArgs e)
string filePath = "C:\\Users\\Admin\\Desktop\\abc.txt";
FileInfo file = new FileInfo(filePath);
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.ContentType = "text/plain";
else Label1.Text = "Requested file is not available to download";
After executing the above code we will be getting output as below. It will have a button which is “Download File”. After clicking on the download file, a pop-up window will appear. It will ask us for the file to download.
You can see here, clicking on the Download button a window will gets open to provide a download file from the server.
We use many keywords in this code. Let us have a look at what exactly all these keywords mean.
- Response.Clear: It will clear the response reference.
- Response.AddHeader: It will specify the file name by adding a header. This method will add the new HTML Header and its value to the response will be sent to the client. It cannot be removed once the header is added.
- Response.ContentType: It will specify the type of content whether it is plain or text provided in the Code. It is used to sets an HTTP header. By using this property we can change the response for the HTTP header.
- Response.Flush: It is used for clearing the flush.
- Response.TransmitFile: It is used for Transmitting the file.it will write the specified part of a File to the HTTP response output stream without buffering it in the memory.
Uploading and downloading functions play an important role in any of the web applications. By using the above example, we can easily download the file. In addition to this, we can also download the file from the server to the local machine. I hope after going through this article, you got knowledge about downloading a file using the asp.net framework.
This is a guide to ASP.NET Download File. Here we discuss the Introduction and how download file works in asp.net along with examples and code implementation. You may also look at the following articles to learn more –
- How to Create RadioButton in ASP.NET?
- Working of Ajax in ASP.NET
- Concept of Data Binding in ASP.NET
- Types of Caching in ASP.NET
- What is .Net? | Framework Architecture
- Learn to ASP.NET LinkButton
- Guide to ImageButton in ASP.NET
- Top 11 Features of ASP.NET
- C# File.Exists | How to Works?
- Guide to Top Examples of ASP.NET ListBox