Skip to content

Code-Quickie: How to do a file download from ASP.Net

February 6, 2007

So you want to let the user save a file that you generate from an ASP.Net page or httphandler?

It’s simple once you know how to set up your response headers:
Response.ClearHeaders();
Response.ContentType = sMimeType;
Response.ContentEncoding = System.Text.Encoding.GetEncoding(sEncoding);
Response.AddHeader( “Content-Disposition”,
“attachment; filename=””+sReportFileName+”.” + sExtension + “””);
Response.CacheControl = “private”;
Response.BinaryWrite(rgBytes);

The most tricky parts to figure out were the Content-Disposition and CacheControl headers. The latter, I only discovered some comparison using Fiddler, which by the way is an indispensable tool for any serious web developer.

Advertisements

From → .Net, C#, Web Development

Leave a Comment

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: