Safari adding .html to download

前端 未结 6 1474
南旧
南旧 2021-02-19 07:17

I have a little function, that creates .xls document(using PHPexcel) and then sends it to php://output. Then user download it.
Everything works fine, except that safari on

相关标签:
6条回答
  • 2021-02-19 07:37

    You can use javascript code

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

    This will open that xls source and file will be available for download

    0 讨论(0)
  • 2021-02-19 07:42

    I have a similar problem and i solved it with the exit function (used parameter status).

    In model:

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

    In controller:

    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)); // <-- 
    }
    
    0 讨论(0)
  • 2021-02-19 07:42

    For anyone having this problem it is the browser doing its own thing. Using JavaScript worked for me but you need to add html than use JavaScript to output to the browser.

    <!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>
    
    0 讨论(0)
  • 2021-02-19 07:47

    If someone is still facing above problem

    Please change content type as

    header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    
    0 讨论(0)
  • 2021-02-19 07:55

    You can try this header:

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

    or check http://www.solutionoferror.com/php/phpexcel-not-downloading-in-mobile-safari-81107.asp .

    0 讨论(0)
  • 2021-02-19 07:57

    I had the same problem

    Resolved with exit; at the end of the script

    $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;
    
    0 讨论(0)
提交回复
热议问题