Download with php with headers do not work on IE8

2020-07-20 04:29发布

问题:

I am trying to download a excel file generated on the fly with php headers:

$filename = "assets.xls";
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=$filename");
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');

But this does not work on IE8 (but on some other pc with IE8 works???!!). IE8 tries to download the export.php file instead of assets.xls. Any idea why IE8 do this?

回答1:

Try to format the header correctly as per the HTTP spec with a space between ; and filename and quotes around the filename:

header('Content-Disposition: attachment; filename="' . $filename . '"');


回答2:

I have encountered the same problem. And I use the followed method to fix the problem.

header("Cache-Control: private");
header("Content-Type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=$filename");


回答3:

I am having a similar issue. I have added the following header before the Content-Deposition header.

header("Content-type: text/csv");
header("X-Download-Options: noopen");
header("Content-Disposition: attachment; filename=\"ExcelFileName.csv\"");

It seems to work for me. However, you have to save the file first. You cannot open right away.



回答4:

I have exact same problem! Just got it to work by removing the 'Content-Type' header, so I guess IE8 doesn't play well with that type..? Not sure yet what the best alternative is, but that definitely is the player for me.



回答5:

Try this:

$filename   =   'Excel_Sheet_'.date('Ymd').".xls";

header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/vnd.ms-excel; "); 
header("Content-Transfer-Encoding: binary");
header('Cache-Control: max-age=0');
ob_clean();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;


回答6:

After trying to fight with a similar issue for a whole afternoon I discovered that setting

header("Cache-Control: private");

Was the best solution. I had already tried ensuring the Content-Length, Content-Type and Content-Disposition were set and correctly formatted. The issue is actually that new IE8 windows and tabs do not seem to like a download sent through PHP headers the first time it occurs. When retrying the file after an initial attempt it works fine (in my cases).

After setting the Cache-Control as mentioned above all my links have worked without a problem in IE8.