通过HTTP标头下载csv文件的问题

时间:2022-10-17 22:08:50

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.

然而,会发生什么,MySQL数据只是回到浏览器。

this is the HTTPheaderResponse i grabbed with firebug:

这是我用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)

EDIT: note it is a return from a JQuery .post() I am trying this, if that affects it.

编辑:注意它是从JQuery返回.post()我正在尝试这个,如果它影响它。

so, does anyone know why this issue may be occuring? I am at a loss.

那么,有谁知道为什么会出现这个问题?我很茫然。

thanks

谢谢

3 个解决方案

#1


2  

First of, be sure to set the second parameter of header() to true, it is to force setting the header:

首先,一定要将header()的第二个参数设置为true,强制设置标题:

header('Content-Type: text/csv');

Give that you know the size of the download, you should:

如果你知道下载的大小,你应该:

header('Content-Length: '.strlen($fields));

Finally, be sure that $fields is actually a string in CSV format.

最后,确保$ fields实际上是CSV格式的字符串。

You can't print an array directly and expect the browser to automagically convert it to CSV.

您无法直接打印数组并希望浏览器自动将其转换为CSV。

#2


1  

Proper MIME type is text/csv. Try that.

正确的MIME类型是text / csv。试试吧。

#3


0  

Try marking the Content-type as either application/download or application/force-download. That should force the browser to open the save dialogue.

尝试将Content-type标记为application / download或application / force-download。这应该强制浏览器打开保存对话框。

#1


2  

First of, be sure to set the second parameter of header() to true, it is to force setting the header:

首先,一定要将header()的第二个参数设置为true,强制设置标题:

header('Content-Type: text/csv');

Give that you know the size of the download, you should:

如果你知道下载的大小,你应该:

header('Content-Length: '.strlen($fields));

Finally, be sure that $fields is actually a string in CSV format.

最后,确保$ fields实际上是CSV格式的字符串。

You can't print an array directly and expect the browser to automagically convert it to CSV.

您无法直接打印数组并希望浏览器自动将其转换为CSV。

#2


1  

Proper MIME type is text/csv. Try that.

正确的MIME类型是text / csv。试试吧。

#3


0  

Try marking the Content-type as either application/download or application/force-download. That should force the browser to open the save dialogue.

尝试将Content-type标记为application / download或application / force-download。这应该强制浏览器打开保存对话框。