Why doesn’t Content-Disposition header work in IE 8?

I’m trying to stream a text file (CSV) to the response, and the following code works perfectly in Firefox 3, but when I use IE, it looks like it wants to download the actual .aspx page, and complains that the file contents don’t match the file extension or type. If I then choose to download the file anyway, it correctly downloads the CSV data and opens it in Excel. What am I doing wrong?

    DataTable dt = ExtensionsProvider.ListPrivateCallCostsForCsv(reportFilter.BusinessUnit, reportFilter.StartDate,
                                                             reportFilter.EndDate);
    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "text/csv";
    Response.AddHeader("Content-Disposition", "filename=" + GetExportFileName()); 
    DataTableHelper.WriteCsv(dt, Response.Output, false);
    Response.End();

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Response.AddHeader("Content-Disposition", "filename=" + GetExportFileName());

Should be:

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

Without a main Content-Disposition value, IE will just use the trailing part of the URL — something.aspx — as a filename.

(The above assumes GetExportFileName() returns a sanitised filename stripped of most punctuation. What can go in a header parameter as token or quoted-string in IE is a matter of some annoyance; see this question for details)

Method 2

It does not work with inline either. Of course it works for all other browsers

HttpServletResponse response = aCtx.getResponse();
response.setContentType("text/plain");
response.addHeader("Content-Disposition", "inline;filename=log.txt");

Method 3

You have to give the value for the Content-Disposition header, in addition to the filename parameter.

You may have more luck with the “inline” value than the “attachment” value:

Response.AddHeader("Content-Disposition", "inline;filename=" + GetExportFileName());


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x