asp.net upload faq
» How to
» Download Source Code
» ASP.NET Upload Control
Quick topical information on how to set up and perform uploading and downloading tasks.
» 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:
» 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:
Here is an example with a 1GB request length and a 10 minute request timeout:
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:
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:
Here is an example with a 1GB request length:
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
» 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:
Access as 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:
» 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.
» 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:
» 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.
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.
© 2013 Krystalware
Painless ASP.NET file uploads