因此,这是可行的:
myphpfile.php:
<?php
header('Content-disposition: attachment; filename=myfile.pdf');
header('Content-type: application/pdf');
readfile('myfile.pdf');
?> 这个php文件在这里被调用,PDF下载工作得很好:
<a class = "oglasavanje" href = "../../cjenik/myphpfile.php">download</a><br/>但这不管用:
<?php
header('Content-disposition: attachment; filename=myfile.xlsx');
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
readfile('myfile.xlsx');
?>.pdf和.xlsx文件都位于同一个文件夹中。当我点击‘下载’链接下载窗口弹出,我保存文件,但文件不能打开。它给出了以下错误:
由于文件格式或文件扩展名无效,
Excel无法打开文件“myfile.xlsx”。验证文件没有损坏,并且文件扩展名是否与文件的格式匹配。
当我手动打开该文件时,它会很好地打开(即不通过链接下载)。
我在用WAMP,如果这很重要的话。
任何帮助都是非常感谢的。
编辑
我在php.net上找到了这段代码:
ob_clean();
flush();所以最后的代码看起来是这样的,它起作用了:
$file = "myfile.xlsx";
header('Content-disposition: attachment; filename='.$file);
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: ' . filesize($file));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
ob_clean();
flush();
readfile($file);谢谢大家的回答。
发布于 2012-04-17 20:39:32
取决于您的浏览器,所以很多事情都会导致这样的行为,如Content-length、Cache-Control等。
也变了
Content-type到Content-Type
Content-disposition到Content-Disposition
试一试
$file = "myfile.xlsx" ;
header('Content-Disposition: attachment; filename=' . $file );
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: ' . filesize($file));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile('myfile.xlsx');发布于 2012-04-17 20:28:04
取决于您的web浏览器,文件名可能不完全保存与句点。如果文件名不完整,可以尝试替换此标题:
header('Content-disposition: attachment; filename=myfile.xlsx');使用
$filename = "myfile.xlsx";
header('Content-Disposition: attachment; filename="' . $filename . '"');发布于 2016-02-04 15:14:36
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($filename);
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: ' . filesize($filename));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile($filename);
return;这个解决方案对我有效,但是当我试图打开下载的文件时,我会得到一个错误。因为.pdf文件只是空的。
https://stackoverflow.com/questions/10198524
复制相似问题