【问题标题】:Can't download a word file无法下载word文件
【发布时间】:2014-03-23 02:45:46
【问题描述】:

你能告诉我我的标题有什么问题吗?我的下载无效。我使用 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;


?>

谢谢

【问题讨论】:

    标签: php download phpword


    【解决方案1】:

    为 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 标题

    【讨论】:

    • 它不起作用(其他答案也不起作用..),你有什么想法吗?
    • 糟糕的是,我使用了一个 POST ajax 请求,它不允许我调用该文件。但是您的回答很好,很有帮助,谢谢!
    【解决方案2】:
    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));
    

    【讨论】:

    • 使用多个内容类型的标题是没有意义的;只有最后一个使用的会实际应用 - 试试这个然后检查浏览器收到的标题以了解我的意思....但是用 OP 的代码修复内容处置文件名问题的荣誉
    【解决方案3】:

    添加两个标题:

    header("Content-Type: application/force-download");
    header("Content-Type: application/download");
    

    【讨论】:

    • 使用多个内容类型的标题是没有意义的;只有最后一个会实际应用 - 试试这个,然后检查浏览器收到的标题以了解我的意思
    【解决方案4】:
                $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); 
    

    【讨论】:

      猜你喜欢
      • 2010-12-28
      • 1970-01-01
      • 2015-07-13
      • 2016-10-08
      • 1970-01-01
      • 2013-10-01
      • 2016-07-29
      • 2013-02-22
      • 1970-01-01
      相关资源
      最近更新 更多