简体   繁体   English

使用PHP / Apache2和Google Chrome下载文件

[英]File Download using PHP/Apache2 and Google Chrome

The download method I am using: 我正在使用的下载方法:

  • I have a "Export" button in HTML which when clicked sends some data to the server. 我在HTML中有一个“导出”按钮,单击该按钮会将一些数据发送到服务器。
  • The server processes the data, writes the CSV to a file and whether the writing was successful or not, sends an appropriate JSON message. 服务器处理数据,将CSV写入文件,并且写入是否成功,发送适当的JSON消息。
  • On receiving a success message, I do a window.open with the proper url and attached file name to start the download. 收到成功消息后,我将执行一个window.open并带有正确的URL和附加文件名以开始下载。
  • The download happens on Firefox in Ubuntu 15.04 alright as well as IE 11 on Windows 7. 下载发生在Firefox在Ubuntu 15.04好吗以及IE 11在Windows 7上。

However the download fails on Google Chrome 45.0.2454.85 m on Windows 7. It just opens a new blank window and then nothing happens. 但是,在Windows 7上的Google Chrome 45.0.2454.85 m上下载失败 。它只是打开一个新的空白窗口,然后什么也没有发生。 I have browser cache cleared. 我已清除浏览器缓存。 What is the right way to start a file download for Chrome? 什么是开始下载Chrome文件的正确方法?

Here is the PHP code responsible for the download. 这是负责下载的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);

Try write all your headers after ob_clean(). 尝试在ob_clean()之后写入所有标头。 ob_clean() clear all data with headers. ob_clean()清除所有带有标题的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM