You are here: Home > Articles > Article Display

Downloading any file to the browser - Part II: using ASP.NET

Following an article on how to do this using ASP 3.0, we'll see how to accomplish the same effect using ASP.NET. Using streams, we can provide a file to the user without the need for FTP or any interference of the Internet Information Server (IIS).

Published: May 22, 2002 | Last Edited: Dec 17, 2002
Tested with: ASP.NET 1.1
Category: ASP.NET
190,846 views

Introduction

Following an article on how to do this using ASP 3.0, we'll see how to accomplish the same effect using ASP.NET. Using streams, we can provide a file to the user without the need for FTP or any interference of the Internet Information Server (IIS).

The Code

Create an ASPX file, called download.aspx. This file takes a filename as a parameter, and returns the contents of that file in the response stream. For example, if you wanted to have the users download a file called xefteri.gif residing in the images folder, you would do so like this:

1 <a href="download.aspx?file=/images/xefteri.gif">Download the Xefteri image</a>

The complete code in VB.NET follows:

1 <%@ Page language="vb" runat="server" explicit="true" strict="true" %>
2 <script language="vb" runat="server">
3 Sub Page_Load(Sender As Object, E As EventArgs)
4     Dim strRequest As String = Request.QueryString("file") '-- if something was passed to the file querystring
5     If strRequest <> "" Then 'get absolute path of the file
6         Dim path As String = Server.MapPath(strRequest) 'get file object as FileInfo
7         Dim file As System.IO.FileInfo = New System.IO.FileInfo(path) '-- if the file exists on the server
8         If file.Exists Then 'set appropriate headers
9             Response.Clear()
10             Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name)
11             Response.AddHeader("Content-Length", file.Length.ToString())
12             Response.ContentType = "application/octet-stream"
13             Response.WriteFile(file.FullName)
14             Response.End 'if file does not exist
15         Else
16             Response.Write("This file does not exist.")
17         End If 'nothing in the URL as HTTP GET
18     Else
19         Response.Write("Please provide a file to download.")
20     End If
21 End Sub
22 </script>

First, we check to see if something was passed in the QueryString, and if nothing was, then we output an error. Then we run our code only if the file actually exists on the web server. All the conditional statements are there to avoid errors in our output. Two headers are needed: one for telling the browser to save the stream as an attachment, and the other to set the length of the stream so that the browser will properly display a download progress bar. The main method here is the WriteFile, which writes the contents of the file in the response stream.

The ContentType header which is added, sets your MIME type so that the browser knows what kind of file this is. You can see a listing of all the MIME types supported on your server, by looking in your web server's properties under MIME types. For example, if you are serving an MS Word file, this would be "application/msword". Octet-stream, which is used above, is the straight binary and acts us the catch-all datatype. When you define something as this datatype, the web server will simply let you download or open the file.

Conclusion

This code should work for all files on Windows systems, but there are some issues with Macintosh systems. Specifically, you may not be able to download the files, instead they will always open up in the browser as expected.

 



Other articles in this category
  1. Smart headers and footers using ASP.NET User Controls
    December 23, 2002
    Good site usability often means removing links from one page back to itself. In this article we will look at how to create an ASP.NET User Control which will act as a common header to a site. It will automatically know which page we are looking at, and it will remove links to the same page from itself. For example, on this site, if we click on the About us section of the header, it will take you to the page, and it will make that link inactive. That way, we know that we are under that section, and we can't click on it anymore.
  2. Maintaining Sorting while Paging in an ASP.NET Datagrid
    December 18, 2002
    The Datagrid server control offers much control and flexibility in presenting data. Two of the actions that are hard-wired into it are Paging and Sorting. On their own they work great, but not so well together. When you sort a column and then move to a previous or next page, the sorting preference is not maintained. In this article we will see how to maintain both by using the Viewstate object.
  3. How postback works in ASP.NET
    December 10, 2002
    In this article, we will take a closer look at how ASP.NET pages post back to themselves, and how to customize this feature in our web applications.
  4. Viewing and editing file and directory attributes in ASP.NET
    December 2, 2002
    The System.IO.FileAttributes class gives us access to file/directory attributes. In this article, we'll see how to use this class to first read the current attributes and then change them.
  5. Copying a directory in ASP.NET
    November 25, 2002
    The System.IO.DirectoryInfo class does not come with a method to copy a directory. In this article, we'll see how to create a method to do that, and then use it in an ASP.NET page.