响应内容类型为CSV

本文翻译自: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中,您可以使用FileContentResultFile方法:

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. 以上将导致出现“另存为”对话框,这可能是您想要的。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值