2011-04-08 102 views
0

我想强制下载。在IE中我得到一个错误,但在FF没有问题文件下载力

header("Content-Type: application/force-download\n"); 
header("Content-Disposition: attachment; filename=test.txt"); 

echo 'test file'; 
+3

你得到哪些错误添加其他的头? – Tobias 2011-04-08 11:44:41

+0

什么'应用程序/强制下载'为MIME类型?你从哪里拿这个?! Oo – KingCrunch 2011-04-08 11:46:19

+0

该文件不可用 – clarkk 2011-04-08 11:48:49

回答

5

我只是用Google搜索你的好奇application/force-download MIME类型。我不知道,谁发明了这个,但看起来,它不存在。见http://mimeapplication.org/force-download.html

这意味着,IE可能不喜欢它。改为使用application/octet-stream。据我记得firefox打开每个MIME类型的下载对话框,这是注册与“显示下载对话框”,或者它根本不知道。在这种情况下,FF可能不知道类型。

+0

它的工作原理,但现在我不能预设文件名... header(“Content-Type:application/octet-stream \ n”); header(“Content-Disposition:attachment; filename = test.txt”); – clarkk 2011-04-08 12:13:38

+0

不知道,如果这是你的行为的原因,但不要在最后换行。 – KingCrunch 2011-04-08 18:13:35

0

这可以帮助你,我的作品

header('Content-Type: application/force-download'); 
header("Content-disposition: attachment; filename=$filename"); 
flush(); 
readfile($filename); 
exit; 

如果你愿意可以:)

+0

在现代服务器代码中,我们应该避免'应用程序/强制下载',这是一个老的黑客。 – 2015-02-04 14:09:09