Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to download file from server in ASP.NET

Author: Rachel Nicole

To download file with a dialog box is simple in ASP.NET. Here is an example to learn this simple approach.

 

You can read articles on DevASP.NET to know about file uploading in ASP.NET. Click here to read one of them.

Many times when you click on a file in a website, it opens in browser. Sometimes a dialog box appears which gives you the option to open or save file in your file system. You need to call AddHeader() method of “Response” object couple of times to download file with this technique. The code snippet provided below will show a dialog box and allows you to save file. Moreover, at the end, you can learn how to download file for specific file type.
 
The code below downloads file in button click event, however you can provide this code in image button click, link button click or in any other event where you want to download your file.
 
VB.NET
 
Imports System.IO
 
Protected Sub ButtonFileDownload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonFileDownload.Click
   Dim URL As String = "~/MyFolder/MyFile.txt"
   Dim fileInfo As New FileInfo(Server.MapPath(URL))
 
   If fileInfo.Exists Then
       Response.Clear()
       Response.AddHeader("Content-Disposition", "attachment; filename =" + fileInfo.Name)
       Response.AddHeader("Content-Length", fileInfo.Length.ToString())
       Response.ContentType = "application/octet-stream"
       Response.Flush()
       Response.WriteFile(fileInfo.FullName)
   End If
End Sub
 
C#
 
using System.IO;
 
protected void ButtonFileDownload_Click(object sender, EventArgs e) {
    string URL = "~/MyFolder/MyFile.txt";
    FileInfo fileInfo = new FileInfo(Server.MapPath(URL));
 
    if (fileInfo.Exists) {
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename =" + fileInfo.Name);
        Response.AddHeader("Content-Length", fileInfo.Length.ToString());
        Response.ContentType = "application/octet-stream";
        Response.Flush();
        Response.WriteFile(fileInfo.FullName);
    }
}
 
 
 
Import required namespace at the top.
 
The file, of course, must have to be there to download. For this example, I have a file “MyFile.txt” in a folder “MyFolder” which exists on the root of my application. “URL” variable have the full path of the file with file name. A “FileInfo” object is created by passing file path in the constructor. Clear() method clears the output from buffer stream. AddHeader() is called providing the name and value as arguments, first time for file name and then for file length. You also have to provide MIME type (Content type) for the file. For the above code, I have used “application/octet-stream” which is the MIME type for binary data files. Flush() sends all buffered output to the client. WriteFile() writes the content of the file to an HTTP response output stream.
 
You can also check specific file extensions and provide their MIME types.

VB.NET

Imports System.IO
 
Protected Sub ButtonFileDownload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonFileDownload.Click
     Dim URL As String = "~/MyFolder/MyFile.txt"
     Dim fileInfo As New FileInfo(Server.MapPath(URL))
 
     If fileInfo.Exists Then
         Response.Clear()
         Response.AddHeader("Content-Disposition", "attachment; filename =" + fileInfo.Name)
         Response.AddHeader("Content-Length", fileInfo.Length.ToString())
 
         Dim type As String = ""
         Dim ext As String = fileInfo.Extension
 
         If ext.ToLower() = ".txt" Then
             type = "text/plain"
         ElseIf ext.ToLower() = ".html" Then
             type = "text/html"
         ElseIf ext.ToLower() = ".pdf" Then
             type = "application/pdf"
         ElseIf ext.ToLower() = ".jpeg" Then
             type = "image/jpeg"
         End If
 
         Response.ContentType = type
 
         Response.Flush()
         Response.WriteFile(fileInfo.FullName)
     End If
End Sub
 
C#
 
using System.IO;
 
protected void ButtonFileDownload_Click(object sender, EventArgs e) {
    string URL = "~/MyFolder/MyFile.txt";
    FileInfo fileInfo = new FileInfo(Server.MapPath(URL));
 
    if (fileInfo.Exists) {
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename =" + fileInfo.Name);
        Response.AddHeader("Content-Length", fileInfo.Length.ToString());
 
        string type = "";
        string ext = fileInfo.Extension;
 
        if (ext.ToLower() == ".txt") {
            type = "text/plain";
        }
        else if (ext.ToLower() == ".html") {
            type = "text/html";
        }
        else if (ext.ToLower() == ".pdf") {
            type = "application/pdf";
        }
        else if (ext.ToLower() == ".jpeg") {
                type = "image/jpeg";
        }
 
        Response.ContentType = type;
           
        Response.Flush();
        Response.WriteFile(fileInfo.FullName);
    }
}
 
All other code is the same except for the MIME type. You can check the file extension and set the MIME type according to the extension. At the end provide the MIME type to “ContentType” property.
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to use Session Object in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net