2

私が達成しようとしていること: ユーザーが選択した pdf ファイルを含む zip ファイルを強制的にダウンロードします。

これを達成するためにコントローラーで行ったこと:

  1. フォルダー APP.WEBROOT_DIR.DS."package_files" に PDF レポートを生成します (私は MPDF ライブラリを使用しました) *正しい読み取り可能な PDF を生成します。ここで $this->render(); と呼びます。

  2. php の Zip 機能を使用して、package.zip (上記の指定したフォルダーからの pdf ファイルで構成されます) を生成します *正しい zip ファイルを生成し、サーバーからダウンロードすると、Windows で有効な zip ファイルとして開きます。

  3. コントローラーのviewClassをMediaに設定し、パラメーターを設定してzipファイルとしてダウンロードを強制します。 *ここでも、ここで$this->render(); 問題: 実行すると zip ファイルが取得されますが、winrar で開くと、Zip ファイルがアーカイブの予期しない終了を報告します。

この問題を解決するのに役立つ記事はありません...

私が推測するのは、レンダリングを2回呼び出すと、ファイルが破損すること です

私のコントローラーコード:

/** before this code i generate pdf files and have no issue **/

/** now scan through the directory and add all the pdf files to a zip archive **/

    $dir = new Folder("".APP.WEBROOT_DIR.DS."package_files");


    $files = $dir->find('.*\.pdf');
    $zip = new ZipArchive();
    foreach ($files as $file) {
        $file_path = $dir->pwd() . DS . $file;


        $filename =  $dir->pwd() . DS ."package.zip";

        if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
            exit("cannot open <$filename>\n");
        }  
        $zip->addFile($file_path,$file);




    }

    $zip->close(); 

/** now render the action to download the generated zip file **/

     $this->viewClass = 'Media';


        $params = array(
            'id'        => 'package.zip',
            'name'      => 'packaged_file',
            'download'  => true,
            'extension' => 'zip',
            'path'      => APP . WEBROOT_DIR.DS.'package_files' . DS
        );
        $this->set($params);
    $this->render();

4

1 に答える 1

0

最初に、Cakephp 2.3 を使用する場合、次の構造を持つ mediaView の代わりに Cake 応答ファイルを使用します。

$this->response->file($file['path']);
// Return response object to prevent controller from trying to render
// a view
return $this->response;

ここにドキュメントがあります:http://book.cakephp.org/2.0/en/controllers/request-response.html#cake-response-file

それ以外の場合は $this->render(); を削除します。アクションの最後に、zip および rar ファイル用に特別に MIME タイプ オプションを指定します。たとえば、docx ファイルには、次のような MIME タイプ オプションを追加します。

// Render app/webroot/files/example.docx
    $params = array(
        'id'        => 'example.docx',
        'name'      => 'example',
        'extension' => 'docx',
        'mimeType'  => array(
            'docx' => 'application/vnd.openxmlformats-officedocument' .
                '.wordprocessingml.document'
        ),
        'path'      => 'files' . DS
    );
于 2014-08-31T19:34:45.227 に答える