如何使用 CSV MIME 类型?

发布于 2024-07-10 04:00:34 字数 387 浏览 6 评论 0原文

在我正在开发的 Web 应用程序中,用户可以单击 CSV 文件的链接。 没有为 mime-type 设置标头,因此浏览器只是将其呈现为文本。 我希望将此文件作为 .csv 文件发送,以便用户可以直接使用 calc、excel、gnumeric 等打开它。

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

此代码在我的计算机上按预期工作(不是一直都是这样吗?)但在另一台计算机上不起作用。

我的浏览器是 FF 3.0.1 的 nightly build(在 Linux 上)。 它无法运行的浏览器是 IE 7 和 FF 3.0(在 Windows 上)

是否有任何我不知道的怪癖?

In a web application I am working on, the user can click on a link to a CSV file. There is no header set for the mime-type, so the browser just renders it as text. 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.

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). The browsers it did not work in were IE 7 and FF 3.0 (on windows)

Are there any quirks I am unaware of?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

初见 2024-07-17 04:00:34

您可以尝试通过执行以下操作来强制浏览器打开“另存为...”对话框:

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

这应该适用于大多数主要浏览器。

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.

会发光的星星闪亮亮i 2024-07-17 04:00:34

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

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

You are not specifying a language or framework, but the following header is used for file downloads:

"Content-Disposition: attachment; filename=abc.csv"
树深时见影 2024-07-17 04:00:34

使用 Internet Explorer,您通常还必须指定 Pragma: public 标头才能使下载正常运行..

header('Pragma: public');

只是我的 2 美分..

With Internet Explorer you often have to specify the Pragma: public header as well for the download to function properly..

header('Pragma: public');

Just my 2 cents..

你是我的挚爱i 2024-07-17 04:00:34

此代码可用于导出任何文件,包括 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"');

This code can be used to export any file, including 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"');
一笔一画续写前缘 2024-07-17 04:00:34

我尝试使用text/csv,但在尝试了不同的东西之后它对我不起作用,我发现如果我们使用这个text/plain。 至此文件上传完成。 正如预期的那样。 这个问题出现在 Yii2 文件上传小部件中。

成功后的示例响应。

{"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"}]}

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. Now the file upload is completed. as expected. This problem was in the Yii2 file upload widget.

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"}]}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文