你能告诉我标题有什么问题吗?我的下载工作正常。我使用PHPWord库,但我认为问题是标题。
<?php
require_once 'PHPWord.php';
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$wordText = utf8_encode($_REQUEST['TEXT']);
$section->addText($wordText);
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
//$objWriter->save('helloWorld.docx');
$path = 'tmp/kikou2.docx';
$objWriter->save($path);
//download code
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$path);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
flush();
readfile($path);
unlink($path);
exit();
?>
下载后,我得到了这样的文件内容:
PK ######## tJWF90G; ##&安培;&LT; ######字/媒体/ section_image1.png&LT;#P&安培;#/ KMNض m ض m nl 7U]3 5 5 } #5'JIA #######HK ### 5Ph z{{1 }})_#
答案 0 :(得分:0)
只需使用此标题即可
header('Content-Type: application/msword'); // for .doc files
或
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); // for .docx files
首先检查上面的标题。该文件应该是可读的。如果不是,则PHPWord_IOFactory :: createWriter()
出现问题Laravel的做法也是:
$objWriter->save($path);
$response = Response::make(Illuminate\Support\Facades\File::get($path), 200);
$response->header('Content-Description', 'File Transfer');
$response->header('Content-Type', 'application/octet-stream');
$response->header('Content-Disposition', 'attachment; filename='.$path);
$response->header('Content-Transfer-Encoding', 'binary');
$response->header('Expires', 0);
$response->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
$response->header('Pragma', 'public');
$response->header('Content-Length', filesize($path));
return $response;