2015-10-15 294 views
1

我想下載這個文件,但我有一些問題。我可以創建並保存它,但在嘗試在瀏覽器上輸出時出現錯誤。phpword通過瀏覽器下載文件

The file xxx.docx cannot be opened because there are problems with the contents. 

其次

Word found unreadable content in xxx.docx. Do you want to recover the contents of this document? 

我點擊是,然後遵循

this file cannot be opened using Microsoft Word. 

我點擊打開,它會打開罰款,沒有任何問題。如果我瀏覽到文件的存儲位置並打開它會打開,所以我相信我的標題設置錯了?

// Saving the document as OOXML file... 
     $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); 
     $objWriter->save(Yii::app()->params['exportToDir'].$filename.".docx"); 
     header("Content-Description: File Transfer"); 
     header('Content-Disposition: attachment; filename="' . $filename . '.docx"'); 
     //header("Content-Type: application/docx"); 
     header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); 
     header('Content-Transfer-Encoding: binary'); 
     header("Cache-Control: public"); 
     header('Expires: 0'); 
     $objWriter->save("php://output"); 

以下適用於Excel。這是一個JSON請求

$filename = "InvoiceSummaryReport_".date("Y-m", strtotime($datestr)) . "_" . date('Y-m-d_H-i-s_T').".xls"; 
     header('Content-Type: application/vnd.ms-excel'); 
     header('Content-Disposition: attachment;filename="'.$filename.'"'); 
     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 

     $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
     $objWriter->save(Yii::app()->params['exportToDir'].$filename); 

     echo "reports/$filename"; 

試圖複製一個字,但它不工作

$filename = "Weekly-MC-Report-" . date('d_F_Y').".docx" ; 
     // Saving the document as OOXML file... 
     header("Content-Description: File Transfer"); 
     header('Content-Disposition: attachment; filename="' . $filename . '"'); 
     header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); 
     header('Content-Transfer-Encoding: binary'); 
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
     header('Expires: 0'); 

     $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); 
     $objWriter->save(Yii::app()->params['exportToDir'].$filename);  
     echo "reports/$filename"; 

回答

2

唯一不同的東西到我的工作定義(見下文)被高速緩存控制部分,即你可以嘗試這些(至少爲了識別引起你的頭痛的行,即使你試圖對你生成的文檔進行一些緩存)...

header("Content-Description: File Transfer"); 
    header('Content-Disposition: attachment; filename="' . $file . '"'); 
    header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); 
    header('Content-Transfer-Encoding: binary'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Expires: 0'); 

    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007'); 
    $objWriter->save('php://output'); 
+0

更新的問題 – shorif2000