如何在 IE 上获取要下载的 csv 文件?在火狐上工作 [英] How do I get csv file to download on IE? Works on firefox
问题描述
我遇到了一个奇怪的错误.我有一个简单的 Web 应用程序,它从数据库中获取内容,然后将其作为可下载的 csv 文件输出.它适用于 firefox 和 chrome,但 IE 无法将其识别为 csv 文件(认为它是 html 文件),当我单击保存时出现错误,无法从 {name of site} 下载 {name of file}. 无法打开此 Internet 站点...."
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. ..."
代码:
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 . "
";
//print the column names
echo "Last Name, First Name, Company
";
while($row = mysql_fetch_assoc($result))
{
echo $row['atlname'] . ',' . $row['atfname'] . ',' . $row['atcompany'] . "
";
}
我已经玩了一大堆内容类型,但没有效果.
I've played around with the content-type a whole bunch, but that had no effect.
更新:我已经尝试过 text/csv、application/vnd.ms-excel(以及它的变体)、text/plain 和其他一些我现在很不走运就忘记的东西.
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.
顺便说一下,这是 IE8.
This is IE8 btw.
更新 2:连接通过 SSL.
Update 2: The connection is over SSL.
推荐答案
我们不喜欢 IE 吗?:)
Don't we love IE? :)
尝试使用这些标题:
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");
我认为八位字节流内容类型会强制 IE 下载文件.
I think that the octet-stream content type forces IE to download the file.
这篇关于如何在 IE 上获取要下载的 csv 文件?在火狐上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!