通过HTTP标头下载csv文件的问题
所以尝试了这个:
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo $fields;
也尝试过和许多其他的。
header("Content-type: text/csv");
然而发生的情况是,MySQL 数据只是回显到浏览器。
这是我用 firebug 抓取的 HTTPheaderResponse:
Date Wed, 18 May 2011 14:15:18 GMT
X-Powered-By PHP/5.1.6
Content-disposition attachment;filename=MyVerySpecial.csv
Connection close
Content-Length 992
Server Apache/2.2.3 (Red Hat)
编辑: 注意它是从 JQuery .post() 返回的,我正在尝试这个,如果这会影响它。
那么,有谁知道为什么会出现这个问题?我不知所措。
谢谢
so attempted this:
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo $fields;
tried also and many others.
header("Content-type: text/csv");
what happens however is, the MySQL data simply echos out to the browser.
this is the HTTPheaderResponse i grabbed with firebug:
Date Wed, 18 May 2011 14:15:18 GMT
X-Powered-By PHP/5.1.6
Content-disposition attachment;filename=MyVerySpecial.csv
Connection close
Content-Length 992
Server Apache/2.2.3 (Red Hat)
EDIT: note it is a return from a JQuery .post() I am trying this, if that affects it.
so, does anyone know why this issue may be occuring? I am at a loss.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,一定要将
header()
的第二个参数设置为true,它是强制设置header:假设你知道下载的大小,你应该:
最后,请确保
$fields
实际上是一个 CSV 格式的字符串。您不能直接打印数组并期望浏览器自动将其转换为 CSV。
First of, be sure to set the second parameter of
header()
to true, it is to force setting the header:Give that you know the size of the download, you should:
Finally, be sure that
$fields
is actually a string in CSV format.You can't print an array directly and expect the browser to automagically convert it to CSV.
正确的 MIME 类型是
text/csv
。尝试一下。Proper MIME type is
text/csv
. Try that.尝试将
Content-type
标记为application/download
或application/force-download
。这应该会强制浏览器打开保存对话框。Try marking the
Content-type
as eitherapplication/download
orapplication/force-download
. That should force the browser to open the save dialogue.