文件下载使用PHP/Apache2和谷歌Chrome


File Download using PHP/Apache2 and Google Chrome

我使用的下载方法:

  • 我在HTML中有一个"导出"按钮,当点击发送一些数据到服务器。
  • 服务器处理数据,将CSV写入文件,无论写入成功与否,发送相应的JSON消息。
  • 在收到成功消息后,我使用正确的url和附加的文件名执行window.open以开始下载。
  • 下载发生在Ubuntu 15.04的Firefox和Windows 7的IE 11上。

但是下载失败在Google Chrome 45.0.2454.85 m在Windows 7上。它只是打开一个新的空白窗口,然后什么也没发生。我有浏览器缓存清除。Chrome下载文件的正确方式是什么?

这是负责下载的PHP代码。
header("Content-Description: File Transfer");
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=". basename($filename));
header("Expires: 0");
header("Cache-Control: must-revalidate");
header("Pragma: public");
header("Content-Length: ". filesize($filename));
ob_clean();
flush();
readFile($filename);

尝试在ob_clean()之后写入所有头文件。Ob_clean()清除所有带有头的数据