簡體   English   中英

無法下載Word文件

[英]Can't download a word file

您能告訴我標頭有什么問題嗎? 我的下載無效。 我使用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('Content-Length: ' . filesize($objWriter));
    readfile($objWriter);
    unlink($objWriter); // deletes the temporary file
    exit;


?>

謝謝

添加兩個標題:

header("Content-Type: application/force-download");
header("Content-Type: application/download");
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename="'.basename($objWriter).'"'); //<<< Note the " " surrounding the file name
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');
header('Content-Length: ' . filesize($objWriter));

推薦OfficeOpenXML .docx文件的標題

// Redirect output to a client’s web browser (.docx)
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment;filename="kikou2.docx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

特別注意Content-Type標題

            $document->save($url.'temp/Form Letters1.docx');
            $path = $url.'temp/Form Letters1.docx'; 
            header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
            header('Content-Disposition: attachment;filename="Form Letters1.docx"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($path));
            ob_clean();
            flush();
            readfile($path); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM