Symfony ZIP文件自动保存,不另存为对话框

时间:2018-08-01 12:31:41

标签: php symfony zip ziparchive

我有一个Action,可以下载选择的文件作为Zip。它可以工作,但是会自动保存zip文件。我想要的是一个对话框(另存为并选择文件夹)。

操作下方:

public function zipDownloadAllDocumentAction( Request $request )
{
    $files = [];

    $path = $request->server->get( 'DOCUMENT_ROOT' ) 
        . $request->getBasePath() 
        . $this->getParameter( "app.path.exports" );

    $documents = $request->get( 'documents' );

    array_push( $files, $path . "/" . $documents[0] );

    $zip     = new ZipArchive();
    $zipName = "Export " . time() . ".zip";
    $zip->open( $zipName, \ZipArchive::CREATE );
    foreach ( $files as $f ) {
        $zip->addFromString( basename( $f ), file_get_contents( $f ) );
    }
    $zip->close();
    $response = new BinaryFileResponse( $zipName );

    $disposition = $response->headers->makeDisposition(
        ResponseHeaderBag::DISPOSITION_ATTACHMENT, $zipName );

    $response->headers->set( 'Content-Disposition', $disposition );

    return $response;
}

0 个答案:

没有答案