本文共 1862 字,大约阅读时间需要 6 分钟。
本文翻译自:
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";
参考:
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/812935header("Cache-Control: must-revalidate");header("Pragma: must-revalidate");header("Content-type: application/vnd.ms-excel");header("Content-disposition: attachment; filename=$filename.csv");
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");}
使用text/csv
作为内容类型。
Try one of these other mime-types (from here: ) 尝试其中一种其他mime类型(从这里: : )
Also, the mime-type might be case sensitive... 此外,mime类型可能区分大小写......
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. 以上将导致出现“另存为”对话框,这可能是您想要的。
转载地址:http://dmlgj.baihongyu.com/