简体   繁体   English

如何使用CSV MIME类型?

[英]How to use the CSV MIME-type?

In a web application I am working on, the user can click on a link to a CSV file.在我正在处理的 web 应用程序中,用户可以单击指向 CSV 文件的链接。 There is no header set for the mime-type, so the browser just renders it as text.没有为 mime 类型设置 header,因此浏览器只是将其呈现为文本。 I would like for this file to be sent as a.csv file, so the user can directly open it with calc, excel, gnumeric, etc.我希望将此文件作为 .csv 文件发送,以便用户可以直接使用 calc、excel、gnumeric 等打开它。

header('Content-Type: text/csv');
echo "cell 1, cell 2";

This code works as expected on my computer (Isn't that how it always is?) but does not work on another computer.此代码在我的计算机上按预期工作(它不是一直这样吗?)但在另一台计算机上不起作用。

My browser is a nightly build of FF 3.0.1 (on linux).我的浏览器是 FF 3.0.1(在 Linux 上)的夜间构建。 The browsers it did not work in were IE 7 and FF 3.0 (on windows)它无法运行的浏览器是 IE 7 和 FF 3.0(在 Windows 上)

Are there any quirks I am unaware of?有什么我不知道的怪癖吗?

You could try to force the browser to open a "Save As..." dialog by doing something like: 您可以尝试通过执行以下操作强制浏览器打开“另存为...”对话框:

header('Content-type: text/csv');
header('Content-disposition: attachment;filename=MyVerySpecial.csv');
echo "cell 1, cell 2";

Which should work across most major browsers. 哪个适用于大多数主流浏览器。

您没有指定语言或框架,但以下标头用于文件下载:

"Content-Disposition: attachment; filename=abc.csv"

With Internet Explorer you often have to specify the Pragma: public header as well for the download to function properly.. 使用Internet Explorer,您通常必须指定Pragma:public header以及下载才能正常运行。

header('Pragma: public');

Just my 2 cents.. 只需我2美分..

This code can be used to export any file, including csv 此代码可用于导出任何文件,包括csv

// application/octet-stream tells the browser not to try to interpret the file
header('Content-type: application/octet-stream');
header('Content-Length: ' . filesize($data));
header('Content-Disposition: attachment; filename="export.csv"');

I tried to use text/csv but it did not work for me after that tried different stuff and I figured out that if we use this text/plain .我尝试使用text/csv但在尝试了不同的东西之后它对我不起作用,我发现如果我们使用这个text/plain Now the file upload is completed.现在文件上传完成了。 as expected.正如预期的那样。 This problem was in the Yii2 file upload widget.此问题出在 Yii2 文件上传小部件中。

Example response after success.成功后的示例响应。

{"files":[{"name":"unit_ids list - Sheet1.csv","type":"text/plain","size":30,"base_url":"https://s3-eu-west-1.amazonaws.com/cdn.abc.co","path":"1/g2qVy3JtyZBLaRUd8c5gMOtSyrTEwdzR.csv","url":"https://s3-eu-west-1.amazonaws.com/cdn.abc.co/1/g2qVy3JtyZBLaRUd8c5gMOtSyrTEwdzR.csv","delete_url":"/coupons/default/sheet-delete?path=1%2Fg2qVy3JtyZBLaRUd8c5gMOtSyrTEwdzR.csv"}]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM