Response Content type as CSV

asp.netHttpCsv

asp.net Problem Overview


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

asp.net Solutions


Solution 1 - asp.net

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.

Solution 2 - asp.net

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

Solution 3 - asp.net

Use text/csv as the content type.

Solution 4 - asp.net

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

Solution 5 - asp.net

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...

Solution 6 - asp.net

Just Use like that

Response.Clear();
Response.ContentType = "application/CSV";
Response.AddHeader("content-disposition", "attachment; filename=\"" + filename + ".csv\"");
Response.Write(t.ToString());
Response.End();

Solution 7 - asp.net

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

Solution 8 - asp.net

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

  

Solution 9 - asp.net

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.

Solution 10 - asp.net

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.

Solution 11 - asp.net

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbalaweblogView Question on Stackoverflow
Solution 1 - asp.netAnthonyWJonesView Answer on Stackoverflow
Solution 2 - asp.netsastaninView Answer on Stackoverflow
Solution 3 - asp.netibzView Answer on Stackoverflow
Solution 4 - asp.netdangerousView Answer on Stackoverflow
Solution 5 - asp.netdiclophisView Answer on Stackoverflow
Solution 6 - asp.netVINKAS IndiaView Answer on Stackoverflow
Solution 7 - asp.netRob ChurchView Answer on Stackoverflow
Solution 8 - asp.netWookieView Answer on Stackoverflow
Solution 9 - asp.netDavid SklarView Answer on Stackoverflow
Solution 10 - asp.netJaiderView Answer on Stackoverflow
Solution 11 - asp.netSuresh KamrushiView Answer on Stackoverflow