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

asp.net

Solution

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)

Problem

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

Original source