ASP.NET Upload Frequently Asked Questions

Complete FAQ for file uploading with ASP.NET 1.x-3.5

How to

Share this page with friends, or bookmark it for yourself:

Quick topical information on how to set up and perform uploading and downloading tasks.

» Set up a html form to accept uploads

» Handle larger uploads and avoid timeouts

» Access an uploaded file

» List all files uploaded to a page

» Set the Content-Type for a download

» Set a file name for a download and show download dialog

» Stream file download

» Set up a html form to accept uploads

To prep a form element to accept uploads, set its enctype attribute to "multipart/form-data". Example:

<form runat="server" enctype="multipart/form-data">

» Handle larger uploads and avoid timeouts

The httpRuntime web.config element controls settings about how ASP.NET processes requests, including maximum request length execution timeouts. These are controlled by the following attributes:

The maximum number of seconds a request is allowed to execute before being automatically shut down by ASP.NET. Defaults to 90 seconds in ASP.NET 1.x, 110 seconds in ASP.NET 2.0 and above.
The maximum allowed request length that will be handled, in kilobytes. Defaults to 4096 (4 MB).

Here is an example with a 1GB request length and a 10 minute request timeout:

<httpRuntime maxRequestLength="1048576" executionTimeout="600" />

For maximum security from denial of service attacks, this setting should be done in a location tag to limit its effect just to the page that can accept uploads. Example:

<location path="Page.aspx">
            executionTimeout="600" />

IIS 7 Configuration

IIS 7 has an additional request filtering module that must be configured seperately. This configuration is controlled by the requestFiltering/requestLimits web.config element. The request size is controlled by the following attribute:

Specifies the maximum length of content in a request, in bytes. The default value is 30000000 (~30MB).

Here is an example with a 1GB request length:

            <requestLimits maxAllowedContentLength="1073741824"/>

If you get an error such as "The requested page cannot be accessed because the related configuration data for the page is invalid" when you add the requestFiltering section to your web.config, you'll have to modify the IIS 7 applicationHost.config file to enable you to configure request filtering at the application level instead of the machine level. To do this, open the %windir%\System32\inetsrv\config\applicationHost.config file in a text editor and change the following line:

<section name="requestFiltering" overrideModeDefault="Deny" />

to read:

<section name="requestFiltering" overrideModeDefault="Allow" />

» Access an uploaded file

Whether you are using the HtmlInputFile html control (supported in .NET 1.0 and on) or the FileUpload webforms control (introduced in .NET 2.0), you can use the HttpPostedFile property to access information about the file, open it as a stream, and/or save it to disk. Examples:

Save to disk:

// Assuming a control named "file"

Access as stream:

// Assuming a control named "file"
using (Stream s = file.PostedFile.InputStream)
    // Do something with the stream...

» List all files uploaded to a page

Use the HttpRequest.Files property to get a collection of all files uploaded during a page request. This property returns an HttpFileCollection that contains all the HttpPostedFile instances representing files that were uploaded. Example:

foreach (HttpPostedFile file in Request.Files)
    Response.Write(file.FileName + "<br />");

» Set the Content-Type for a download

To control the content type the browser assumes it's getting, set the Request.ContentType property. This sets the HTTP Content-Type header. The value should be a MIME type or "internet media type" that identifies the type of file being downloaded.

The following code sets the content type to a JPEG image.

Response.ContentType = "image/jpeg";

» Set a file name for a download and show download dialog

To set the filename for a download and force the download box to be shown, use the HTTP Content-Disposition header. Specify the filename, and "attachment" to display the download box. Example:

Response.AddHeader("Content-Disposition", "attachment;filename=" + "theFilename.ext");

» Stream file download

When working with filesystem downloads in ASP.NET 1.1 and above, scalable streaming is easy. Simply delegate the task to ASP.NET by calling the Response.WriteFile method:


If you are using ASP.NET 1.0, or need to write data from something other than a file, you need the following method. It copies one stream to another by reads and writing chunks instead of loading the entire stream into memory as is shown in many examples.

public void CopyStreamToStream(Stream inStream, Stream outStream)
    byte[] buffer = new byte[8192];

    int read;

    while ((read = inStream.Read(buffer, 0, buffer.Length)) > 0)
        outStream.Write(buffer, 0, read);

This method uses an 8KB buffer. Depending on the sizes of files and source and destination, you may want to change the chunk size.

To use this method to write a file to the response in ASP.NET 1.0, see the following example.

using (Stream s = File.OpenRead("serverfilename.bin"))
    CopyStreamToStream(s, Response.OutputStream);
SlickUpload ASP.NET Upload Component


Painless ASP.NET file uploads

  • Handle large files
  • Upload with progress bar
  • Gracefully handle cancellation and errors
  • Stream directly to file or database
  • Drag-drop simple, yet highly customizable
  • Solid, responsive support

» more info | live demo | download