php 如何在 IE 上获取要下载的 csv 文件?在火狐上工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2232103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get csv file to download on IE? Works on firefox
提问by tau-neutrino
I'm struggling with an odd error. I have a simple web app that grabs stuff from a DB then outputs it as a downloadable csv file. It works on firefox and chrome, but IE fails to recognize it as a csv file (thinking it is a html fle) and when I click save I get the error, "Unable to download {name of file} from {name of site}. Unable to open this internet site. ..."
我正在为一个奇怪的错误而苦苦挣扎。我有一个简单的 Web 应用程序,它从数据库中获取内容,然后将其作为可下载的 csv 文件输出。它适用于 firefox 和 chrome,但 IE 无法将其识别为 csv 文件(认为它是 html 文件),当我单击保存时出现错误,“无法从 {name of site} 下载 {name of file} . 无法打开这个网站。......”
Code:
代码:
session_start();
//some logic goes here...
//generate csv header
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=exportevent.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo "Event: " . $event_title . "\n";
//print the column names
echo "Last Name, First Name, Company \n";
while($row = mysql_fetch_assoc($result))
{
echo $row['atlname'] . ',' . $row['atfname'] . ',' . $row['atcompany'] . "\n";
}
I've played around with the content-type a whole bunch, but that had no effect.
我已经玩了一大堆内容类型,但这没有效果。
Update: I've tried text/csv, application/vnd.ms-excel (and variations of this), text/plain, and some others that I now forget with no luck.
更新:我已经尝试过 text/csv、application/vnd.ms-excel(以及它的变体)、text/plain 和其他一些我现在忘记了但没有运气的东西。
This is IE8 btw.
这是 IE8 顺便说一句。
Update 2: The connection is over SSL.
更新 2:连接通过 SSL。
回答by Caissy
Don't we love IE? :)
我们不爱IE吗?:)
Try using those headers:
尝试使用这些标题:
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"exportevent.csv\";" );
header("Content-Transfer-Encoding: binary");
I think that the octet-stream content type forces IE to download the file.
我认为八位字节流内容类型强制 IE 下载文件。
回答by jasonbar
We recently ran into this problem ourselves. See this MSKB article
我们最近自己也遇到了这个问题。请参阅此MSKB 文章
These are the headers we ended up having to use to get it to work over SSL.
这些是我们最终必须使用的标头才能让它通过 SSL 工作。
header("Expires: Sat, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$file_name\";");
header("Content-length: " . strlen($csv_string));
回答by Randoogle
The only extra code I had to add for IE to work with SSL was: header("Pragma: public");So my headers look like this now:
为了让 IE 使用 SSL,我必须添加的唯一额外代码是:header("Pragma: public");所以我的标头现在看起来像这样:
header("Pragma: public");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=some_filename.csv");
回答by Jason
I've had success with the following:
我在以下方面取得了成功:
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=File.csv");
Setting the type to application/vnd.ms-excel seemed to do the trick in my case. This is all in a file that is opened by submitting a form using
在我的情况下,将类型设置为 application/vnd.ms-excel 似乎可以解决问题。这一切都在通过使用提交表单打开的文件中
target="_blank"
回答by davidu
We have just had the same issue and after adding many headers and getting a working link I then removed them one by one and found the key one for us was "Cache-Control: public" so in the end we just had
我们刚刚遇到了同样的问题,在添加了许多标题并获得了一个工作链接之后,我将它们一个一个地删除,发现对我们来说关键的是“Cache-Control:public”,所以最后我们只有
header("Cache-Control: public");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=some_filename.csv");
which worked fine.
效果很好。
回答by Seth
Try setting your content type to text/csvinstead of application/octet-stream.
尝试将您的内容类型设置为text/csv而不是application/octet-stream。
Since application/octet-stream is a generic binary mime type (and doesn't match the '.csv' extension), Internet explorer might be ignoring it and computing the mime type based on the file extension.
由于 application/octet-stream 是通用的二进制 mime 类型(并且与“.csv”扩展名不匹配),Internet Explorer 可能会忽略它并根据文件扩展名计算 mime 类型。
回答by Frankos
The solution for me was:
我的解决方案是:
header_remove();
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename=brokerlist.csv');
echo $content;
回答by Ardee Aram
This simply doesn't make sense. I tried the accepted answer, all the other answers in here, and it didn't work for me. I tried their permutations, and somehow I managed to make it work in IE like so:
这根本没有意义。我尝试了接受的答案,这里的所有其他答案,但它对我不起作用。我尝试了他们的排列,不知何故我设法让它在 IE 中像这样工作:
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Type: application/vnd.ms-exce");
header("Content-Disposition: attachment; filename=coupons.csv" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . strlen($csv));
echo $csv;
die();
One thing I did is to empty the cache every freaking time I test the code. And it still doesn't make sense. Just in case someone might need this desperately ;)
我所做的一件事是每次测试代码时都清空缓存。它仍然没有意义。以防万一有人可能迫切需要这个;)
回答by dev-null-dweller
Some time ago I've got a problem with IE6 opening pdf files, and crashing when AdobeReader 6.0 was installed and tried to open file in browser window. Than I found somewhere this header:
前段时间我遇到了 IE6 打开 pdf 文件的问题,并且在安装 AdobeReader 6.0 并尝试在浏览器窗口中打开文件时崩溃。比我在某个地方找到了这个标题:
header('Content-Type: application/force-download');
And it solved the problem, every pdf file was downloaded and opened in Adobe instead of IE.
它解决了这个问题,每个pdf文件都是在Adobe而不是IE中下载和打开的。
回答by Dominik
Did you try the Content-type: text/csv ?
您是否尝试过 Content-type: text/csv ?

