Safari adding .html to download

浪尽此生 提交于 2019-12-06 18:27:54

问题


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 mac os x adds .html extension for some reason.
So the resulted file is named report.xls.html. Content is ok, but it is annoying to the users.

How can I resolve this?

Here is part of my code:

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

回答1:


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;



回答2:


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 .




回答3:


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



回答4:


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




回答5:


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>



回答6:


If someone is still facing above problem

Please change content type as

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


来源:https://stackoverflow.com/questions/19363744/safari-adding-html-to-download

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!