【问题标题】:Header when download file using phpword使用phpword下载文件时的标题
【发布时间】:2015-02-23 02:32:54
【问题描述】:

你能告诉我我的标题有什么问题吗?我的下载工作正常。我使用 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#######tJWF90�G�;##&J?J�wr��v###���@��#�##b�����g�e�E�/Mw��gS�l��#��-�����2�#\#�"n5��G�W�G�|#X+# \��B��Ks��#�#@��[��y�7�����j��Êh�|#BA�^#49j�[��Gv�#��#��#�#z>짙C���v)�� ��_��#��

【问题讨论】:

    标签: php css laravel


    【解决方案1】:

    只使用这个标题应该可以工作

    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;
    

    【讨论】:

    • 我尝试按照您上面的所有解决方案进行操作,但文件的内容仍然像以前一样。是的,我正在使用 Laravel。
    • 如果你得到象形文字时只是标题 "header('Content-Type: application/msword');"使用过您的文件可能已损坏。正如我所说,检查 PHPWord_IOFactory 文档。
    猜你喜欢
    • 2018-05-05
    • 2013-10-01
    • 2015-03-16
    • 2012-07-13
    • 2022-10-12
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多