3

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();
ProfK
  • 49,207
  • 121
  • 399
  • 775

3 Answers3

9
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)

Community
  • 1
  • 1
bobince
  • 528,062
  • 107
  • 651
  • 834
0

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");
Alberto Godar
  • 249
  • 1
  • 7
-1

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());
brianary
  • 8,996
  • 2
  • 35
  • 29