Home > 2010 Winter BTI420 > File upload to, and download from, an ASP.NET Web Forms app

File upload to, and download from, an ASP.NET Web Forms app

January 28, 2010 Leave a comment Go to comments

This post shows you how to get file upload and file download working for an ASP.NET Web Forms app.



File upload – TO – a web app

Now that we know that we use impersonation to enable create/modify/delete operations on files and folders, we will cover the file upload topic.

ASP.NET includes a "FileUpload" web server control in its Standard toolbox. It includes a textbox and a button that lets the web browser user navigate/browse the user’s local file system to select a single file object to upload. A default setting in the ASP.NET runtime prevents the upload of a file which exceeds 4MB in size, but this setting can be overridden.

To upload successfully, you must be aware of a few key concepts:

  • The FileUpload control permits the selection and uploading of a single file object (i.e. one-at-a-time file uploads)
  • You must add a command button web server control to enable to user to complete the file upload activity
  • In BTI420, you must use impersonation to permit the host page to write to the file system on the server running your web app
  • You must have knowledge of ASP.NET Web Site Paths; both virtual and physical

See today’s code example "…FileUpload1".

In Lab 4, you will have an opportunity to work with an uploaded file’s properties, including size/length, MIME type, and so on.


File download – FROM – a web app

When a user interacts with a web site, the user is "downloading" content by selecting links and/or performing operations.

The web server, together with the ASP.NET runtime, take care of this task. For example, when a user clicks on a link that resolves to an XHTML document (e.g. .htm, .html, .aspx, etc.), the web server informs the web browser that "text/html" content will be sent, so that the web browser can handle the content correctly. For another example, if the content to be received is an image (e.g. a .jpg or .jpeg), the web server informs the web browser that "image/jpeg" content will be sent, so (again) the web browser can handle the content correctly.

In this file download topic, we will intercede and manage the "download" process, and introduce you to a few fundamental web server objects. We will manually set the content type ourselves, and specify the disposition (display/process in band / inline, or prompt for download).

The HttpResponse class encapsulates HTTP response information from an ASP.NET operation. The methods and properties of the HttpResponse class are exposed through the Response property of the HttpApplication, HttpContext, Page, and UserControl classes. Therefore, when working in C# code in a web form’s partial class, its reference will be "Response" (as a property of the Page class).

Please note that this topic isn’t really well supported by how-to examples in the MSDN documentation. The topic is not unimportant, but its application and usage is fairly common. When manipulating the content that gets sent to browser users, please refer to the documentation of the following HttpResponse class members for information and code examples:

  • AppendHeader
  • Clear
  • Write (we’ve used this before)
  • WriteFile
  • ContentType
  • OutputStream

Our simple example will create a web form that has links to different file types. See today’s code example "…FileDownload1" for more.

Here are some example usage scenarios for the Response property:

View content using the default server AND browser settings

No configuration is required. You do not have to configure the Response property’s members.

Send content as a specific MIME type stream

Configure the following:

  • Set the ContentType
  • Clear the HTTP output buffer
  • Send the file

Send content and prompt (i.e. show a dialog) for saving it

Configure the following:

  • Set the ContentType
  • Set the Content-Disposition header
  • Send the file


Categories: 2010 Winter BTI420
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: