ProfK
ProfK

Reputation: 51064

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();

Upvotes: 3

Views: 12549

Answers (3)

Alberto Godar
Alberto Godar

Reputation: 249

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");

Upvotes: 0

bobince
bobince

Reputation: 536379

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)

Upvotes: 9

brianary
brianary

Reputation: 9322

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());

Upvotes: -1

Related Questions