Richard Ev
Richard Ev

Reputation: 54117

Intermittent bug - IE6 showing file as text in browser, rather than as file download

In an ASP.NET WebForms 2.0 site we are encountering an intermittent bug in IE6 whereby a file download attempt results in the contents of the being shown directly in the browser as text, rather than the file save dialog being displayed. Our application allows the user to download both PDF and CSV files.

The code we're using is:

HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.AddHeader("Content-Disposition", "attachment;filename=\"theFilename.pdf\"");
response.ContentType = "application/pdf";
response.BinaryWrite(MethodThatReturnsFileContents());
response.End();

This is called from the code-behind click event handler of a button server control.

Where are we going wrong with this approach?

Edit

Following James' answer to this posting, the code I'm using now looks like this:

HttpResponse response = HttpContext.Current.Response;
response.ClearHeaders();
// Setting cache to NoCache was recommended, but doing so results in a security
// warning in IE6
//response.Cache.SetCacheability(HttpCacheability.NoCache);
response.AppendHeader("Content-Disposition", "attachment; filename=\"theFilename.pdf\"");
response.ContentType = "application/pdf";
response.BinaryWrite(MethodThatReturnsFileContents());
response.Flush();
response.End();

However, I don't believe that any of the changes made will fix the issue.

Upvotes: 2

Views: 293

Answers (1)

James Walford
James Walford

Reputation: 2953

The glib answer is you're going wrong by supporting IE6....

Response.Clear only clears content from the response, use Response.ClearHeaders instead.

Other than that, you might want to look at using Response.Buffer & Response.Flush and explicitly setting

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Personally I might want to add content length and charset to my headers as the more info the browser has to work with the better.

http://msdn.microsoft.com/en-us/library/system.web.httpresponse.aspx

Upvotes: 1

Related Questions