【问题标题】:Safari adding .html to downloadSafari 添加 .html 下载
【发布时间】:2013-10-14 15:28:12
【问题描述】:

我有一个小函数,它创建 .xls 文档(使用 PHPexcel),然后将其发送到 php://output。然后用户下载它。
一切正常,除了 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');

【问题讨论】:

    标签: php safari download xls


    【解决方案1】:

    我遇到了同样的问题

    通过退出解决;在脚本的末尾

    $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;
    

    【讨论】:

    • 感谢上帝,这个解决方案有效,但请问为什么exit; 解决了这个问题?
    • 有谁知道为什么 exit() 可以解决这个问题?
    【解决方案2】:

    你可以试试这个标题:

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

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

    【讨论】:

    • 你可以使用javascript window.location.href = "stackoverflow.com/file.xls";并打开那个 xls 源
    • 嗯,它有点工作。不是很优雅,但是是的,如果我先保存文件,然后将浏览器指向它 - 它会保存为 .xls。您可以将其发布为答案,以便我接受吗?
    【解决方案3】:

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

    在模型中:

    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)); // <-- 
    }
    

    【讨论】:

    • 你知道为什么 exit() 可以解决这个问题吗?
    【解决方案4】:

    你可以使用 javascript 代码

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

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

    【讨论】:

      【解决方案5】:

      对于遇到此问题的任何人来说,是浏览器在做自己的事情。使用 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>
      

      【讨论】:

        【解决方案6】:

        如果有人仍然面临上述问题

        请将内容类型改为

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

        【讨论】:

          猜你喜欢
          • 2017-03-23
          • 2014-08-20
          • 2011-02-13
          • 1970-01-01
          • 2017-07-18
          • 2018-03-13
          • 1970-01-01
          • 1970-01-01
          • 2015-07-27
          相关资源
          最近更新 更多