Safari adding .html to download

青春壹個敷衍的年華 提交于 2019-12-05 01:28:48

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;
sourabh kasliwal

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 .

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

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

Tony Ramirez

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>

If someone is still facing above problem

Please change content type as

header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!