本文翻译自:Response Content type as CSV
I need to send a CSV file in HTTP response. 我需要在HTTP响应中发送CSV文件。 How can I set the output response as CSV format? 如何将输出响应设置为CSV格式?
This is not working: 这不起作用:
Response.ContentType = "application/CSV";
#1楼
参考:https://stackoom.com/question/1eP9/响应内容类型为CSV
#2楼
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");
#3楼
In ASP.net MVC, you can use a FileContentResult
and the File
method: 在ASP.net MVC中,您可以使用FileContentResult
和File
方法:
public FileContentResult DownloadManifest() {
byte[] csvData = getCsvData();
return File(csvData, "text/csv", "filename.csv");
}
#4楼
使用text/csv
作为内容类型。
#5楼
Try one of these other mime-types (from here: http://filext.com/file-extension/CSV ) 尝试其中一种其他mime类型(从这里: http : //filext.com/file-extension/CSV )
- text/comma-separated-values 文本/逗号分隔值
- text/csv 文/ CSV
- application/csv 应用程序/ CSV
- application/excel 应用程序/ EXCEL
- application/vnd.ms-excel 应用/ vnd.ms-Excel中
- application/vnd.msexcel 应用程序/ vnd.msexcel
Also, the mime-type might be case sensitive... 此外,mime类型可能区分大小写......
#6楼
Using text/csv
is the most appropriate type. 使用text/csv
是最合适的类型。
You should also consider adding a Content-Disposition
header to the response. 您还应该考虑在响应中添加Content-Disposition
标头。 Often a text/csv will be loaded by a Internet Explorer directly into a hosted instance of Excel. 通常,Internet Explorer将text / csv直接加载到托管的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. 以上将导致出现“另存为”对话框,这可能是您想要的。