博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
响应内容类型为CSV
阅读量:3584 次
发布时间:2019-05-20

本文共 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";

#1楼

参考:


#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/812935header("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: ) 尝试其中一种其他mime类型(从这里: : )

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

转载地址:http://dmlgj.baihongyu.com/

你可能感兴趣的文章
hadoop3.0+spark2.0两台云服务器集群环境配置。
查看>>
网站实现qq登录(springboot后台)
查看>>
简单的用户头像修改功能(springboot后台)
查看>>
springboot+mybatis实现分页
查看>>
为什么局域网网段不同不能通信?
查看>>
认识和使用JWT
查看>>
条件表达式于运算符的点点滴滴的积累
查看>>
最短路径最基本的三种算法【此后无良辰】
查看>>
class的点点滴滴的总结
查看>>
vector 的点点滴滴的总结
查看>>
测试用例
查看>>
自动化测试学习步骤
查看>>
自动化测试需要掌握的知识
查看>>
HTTP协议
查看>>
Python小程序——冒泡排序
查看>>
cmd中输入net start mysql 提示:服务名无效或者MySQL正在启动 MySQL无法启动
查看>>
LeetCode 206反转链表 [javsScript]
查看>>
[LeetCode javaScript] 3. 无重复字符的最长子串
查看>>
[LeetCode javaScript] 6. Z字形变换
查看>>
[LeetCode javaScript]455. 分发饼干
查看>>