Safari添加.html下载

时间:2013-10-14 15:28:13

标签: php safari download xls

我有一个小函数,创建.xls文档(使用PHPexcel),然后将其发送到php://输出。然后用户下载它。
一切正常,除了mac os x上的safari由于某种原因增加了.html扩展名。
因此,生成的文件名为report.xls.html。内容没问题,但对用户来说很烦人。

我该如何解决这个问题?

以下是我的代码的一部分:

$filename = 'report.xls';

header('Content-Description: File Transfer');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="'.$filename.'"'); 
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');

$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save('php://output');

6 个答案:

答案 0 :(得分:28)

我遇到了同样的问题

退出决定;在脚本的末尾

$filename = 'report.xls';
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="'.$filename.'"'); 
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');

$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save('php://output');
exit;

答案 1 :(得分:1)

您可以尝试此标题:

header('Content-type: application/ms-excel');

或检查 http://www.solutionoferror.com/php/phpexcel-not-downloading-in-mobile-safari-81107.asp

答案 2 :(得分:0)

你可以使用 javascript代码

<script>
 window.location.href = "stackoverflow.com/file.xls";
</script>

这将打开xls源和文件可供下载

答案 3 :(得分:0)

对于遇到此问题的任何人来说,浏览器都在做自己的事情。使用JavaScript对我有用,但你需要添加html而不是使用JavaScript输出到浏览器。

<!doctype html>
<html>
     <head> <meta charset="UTF-8"> <title>Untitled  ocument</title> </head> 
     <body> 
          <script> window.location.href = http://example/file.docx"; </script> 
     </body>
</html>

答案 4 :(得分:0)

如果有人仍面临上述问题

请将内容类型更改为

header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

答案 5 :(得分:0)

我有一个类似的问题,我用退出功能(使用的参数状态)解决了。

在模型中:

public static function xls($id)
{
    $xls = new PHPExcel();
    //Code ...
    $objWriter = new \PHPExcel_Writer_Excel5($xls);

    ob_start();
    $objWriter->save('php://output');
    $excelOutput = ob_get_clean();

    return $excelOutput;
}

在控制器中:

public function xls($id)
{
    header('Expires: Mon, 1 Apr 1974 05:00:00 GMT');
    header('Last-Modified: ' . gmdate('D,d M YH:i:s') . ' GMT');
    header('Cache-Control: no-cache, must-revalidate');
    header('Pragma: no-cache');
    header('Content-type: application/vnd.ms-excel');
    header('Content-Disposition: attachment; filename=' . $id . '.xls');

    return exit(Controller::xls($id)); // <-- 
}