如何使用 CSV MIME 类型?
在我正在开发的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以尝试通过执行以下操作来强制浏览器打开“另存为...”对话框:
这应该适用于大多数主要浏览器。
You could try to force the browser to open a "Save As..." dialog by doing something like:
Which should work across most major browsers.
您没有指定语言或框架,但以下标头用于文件下载:
You are not specifying a language or framework, but the following header is used for file downloads:
使用 Internet Explorer,您通常还必须指定 Pragma: public 标头才能使下载正常运行..
只是我的 2 美分..
With Internet Explorer you often have to specify the Pragma: public header as well for the download to function properly..
Just my 2 cents..
此代码可用于导出任何文件,包括 csv
This code can be used to export any file, including csv
我尝试使用
text/csv
,但在尝试了不同的东西之后它对我不起作用,我发现如果我们使用这个text/plain
。 至此文件上传完成。 正如预期的那样。 这个问题出现在 Yii2 文件上传小部件中。成功后的示例响应。
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 thistext/plain
. Now the file upload is completed. as expected. This problem was in the Yii2 file upload widget.Example response after success.