我有一个脚本可以在将表单提交到数据库后下载pdf,但是当我在Safari或Explorer上下载pdf时,我的pdf是空的,无法打开,我已经尝试了很多事情,现在我使用JavaScript whit window.location,它适用于Safari和Explorer,但我真的希望我的PHP脚本适用于所有浏览器
<?php
ob_start();
if ($_REQUEST["action"] == "download") {
$file = base64_decode($_REQUEST["download"]);
//Insert to download table....
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$agent = $_SERVER['HTTP_USER_AGENT'];
}
if (strlen(strstr($agent, 'Firefox')) > 0) {
$browser = 'firefox';
} else if (strlen(strstr($agent, 'Chrome')) > 0) {
$browser = 'chrome';
} else {
$browser = 'safari';
}
if ($browser == 'safari'){
?>
<script type="text/javascript">
window.location = '<?php echo $file;?>';
</script>
<?php
} else { ?>
<?php
if ($file) {
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: application/octet-stream');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($file)) . ' GMT');
header('Content-Disposition: attachment; filename='.basename($file));
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($file)); // provide file size
header('Connection: close');
readfile($file);
exit();
}
}
}
我通过删除一些标头解决了这个问题,现在我只使用它
<?php
ob_start();
if ($_REQUEST["action"] == "download") {
$file = base64_decode($_REQUEST["download"]);
if ($file) {
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header("Content-disposition: attachment; filename=".basename($file));
header("Content-type: application/pdf");
readfile($file);
exit();
}
}
如果要
在资源管理器中发布此类文档,则应使用函数ob_flush();