Response Content type as CSV
Asked Answered
D

11

400

I need to send a CSV file in HTTP response. How can I set the output response as CSV format?

This is not working:

Response.ContentType = "application/CSV";
Disinfect answered 26/12, 2008 at 9:32 Comment(0)
L
580

Using text/csv is the most appropriate type.

You should also consider adding a Content-Disposition header to the response. Often a text/csv will be loaded by a Internet Explorer directly into a hosted instance of Excel. This may or may not be a desirable result.

Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.csv");

The above will cause a file "Save as" dialog to appear which may be what you intend.

Laaspere answered 26/12, 2008 at 11:26 Comment(1)
I like to use a System.Net.Mime.ContentDisposition object for building that string.Pentapody
I
205

MIME type of the CSV is text/csv according to RFC 4180.

Idol answered 16/3, 2009 at 18:47 Comment(0)
C
84

Use text/csv as the content type.

Coltson answered 26/12, 2008 at 9:36 Comment(0)
H
55

Over the years I've been honing a perfect set of headers for this that work brilliantly in all browsers that I know of

// these headers avoid IE problems when using https:
// see http://support.microsoft.com/kb/812935
header("Cache-Control: must-revalidate");
header("Pragma: must-revalidate");

header("Content-type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=$filename.csv");
Hornbeck answered 12/11, 2009 at 1:11 Comment(2)
If you use application/vnd.ms-excel. On osx safari adds a ".xls" file extensionNolde
The Pragma header is deprecated by now.Thor
M
30

Try one of these other mime-types (from here: http://filext.com/file-extension/CSV )

  • text/comma-separated-values
  • text/csv
  • application/csv
  • application/excel
  • application/vnd.ms-excel
  • application/vnd.msexcel

Also, the mime-type might be case sensitive...

Mannequin answered 26/12, 2008 at 9:37 Comment(0)
M
15

Just Use like that

Response.Clear();
Response.ContentType = "application/CSV";
Response.AddHeader("content-disposition", "attachment; filename=\"" + filename + ".csv\"");
Response.Write(t.ToString());
Response.End();
Maddock answered 28/4, 2009 at 12:54 Comment(2)
Enclosing the file name in double quotes fixed this for me when the client is Firefox.Sarcenet
If you're using this in an MVC controller, you'll need to end the controller method with return new EmptyResult() to get the filename to stick. Otherwise IE will try and save the file as ActionName.htm.Antipode
R
5

In ASP.net MVC, you can use a FileContentResult and the File method:

public FileContentResult DownloadManifest() {
    byte[] csvData = getCsvData();
    return File(csvData, "text/csv", "filename.csv");
}
Rogovy answered 25/7, 2013 at 11:35 Comment(0)
E
4

I have found that the problem with IE is that it sniffs the return data and makes up its own mind about what content-type it thinks it has been sent. There are a number of side effect that this causes, such as always openning a saveAs dialog for text files because you are using compression of data trasnferes. The solution is (in php code)......

header('X-Content-Type-Options: nosniff');
Exsect answered 16/2, 2012 at 12:20 Comment(0)
G
2

I suggest to insert an '/' character in front of 'myfilename.cvs'

Response.AddHeader("Content-Disposition", "attachment;filename=/myfilename.csv");

I hope you get better results.

Griffey answered 13/6, 2011 at 21:59 Comment(0)
A
1

Setting the content type and the content disposition as described above produces wildly varying results with different browsers:

IE8: SaveAs dialog as desired, and Excel as the default app. 100% good.

Firefox: SaveAs dialog does show up, but Firefox has no idea it is a spreadsheet. Suggests opening it with Visual Studio! 50% good

Chrome: the hints are fully ignored. The CSV data is shown in the browser. 0% good.

Of course in all of these cases I'm referring to the browsers as they come out of they box, with no customization of the mime/application mappings.

Ader answered 28/4, 2009 at 1:39 Comment(3)
If Firefox wants you to open with Visual Studio, means you are exporting the HTML and not a CSV.Jigger
This does not appear to be the case in 2014, it worked fine in all of them for me.Pily
Please define "as described above"Hubblebubble
S
0

For C# MVC 4.5 you need to do like this:

Response.Clear();
Response.ContentType = "application/CSV";
Response.AddHeader("content-disposition", "attachment; filename=\"" + fileName + ".csv\"");
Response.Write(dataNeedToPrint);
Response.End();
return new EmptyResult();  //this line is important else it will not work.
Softener answered 27/3, 2018 at 11:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.