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();
Should be:
Without a main
Content-Dispositionvalue, 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)