强制下载文件
我编写了一个脚本,将数据添加到现有的 csv 文件中。 对于其他用户来说,它也可以正常工作,但对于一个用户来说,文件不可下载,只能在浏览器中打开。
写入文件时使用的标头:
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=\"file.csv\";" );
header("Content-Transfer-Encoding: binary");
我通过 ajax 发送一些用户选择,并在准备好后导航到文件。
$.get( urlJson ,function(){
document.location.href = urlCsv;
});
我认为这是服务器问题,也许有些东西没有启用,但我不知道到底是什么。
I've wrote a script which add data to already existing csv file.
It is working ok for other users too, but for one user file is not downloadable only opens in browsers.
Header used when writing to file:
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=\"file.csv\";" );
header("Content-Transfer-Encoding: binary");
I'm sending some user choise by ajax and when it is ready navigate to the file.
$.get( urlJson ,function(){
document.location.href = urlCsv;
});
I think this is server problem maybe something is not enable, but I don't know what exactly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该可以做到
This should do it
使用 PHP 强制下载最简单的情况是
,用户浏览器可能只是忽略下载文件的请求(因为这取决于浏览器要做什么),或者可能不知道 application/octet-stream 是什么。
您还有更多信息吗?
CSV 可以是“文本/逗号分隔值、文本/csv、应用程序/csv”,因为它是 MIME 您尝试过这些吗?
Forcing a download with PHP at its simplest is
It is possible the users browser is just ignoring the request to download the file (as it's up to the browser what to do) or perhaps it doesn't know what application/octet-stream is.
Do you have any more information?
A CSV can be "text/comma-separated-values, text/csv, application/csv" as it's MIME have you tried these?
类似于@Walkerneo所说的......
但是,某些版本的 Internet Explorer 喜欢选择对
application/octet-stream
执行的操作。例如,在我的网站之一上,IE 总是会在媒体播放器中打开 mp3 文件。解决方案是在.htaccess
文件中添加AddType mycompany/podcast mp3
。任何像这样的组合类型都会强制 IE 显式下载。Similar to what @Walkerneo said...
However, some versions of Internet Explorer like to choose what they do with an
application/octet-stream
. For instance on one of my websites IE would always open mp3 files in media player. The solution was to addAddType mycompany/podcast mp3
in the.htaccess
file. Any made up type like this will force IE to explicitly download.