使用symfony2下载多张图片

时间:2014-02-21 08:56:01

标签: jquery image symfony download zip

我想在我的symfony2项目中使用jquery下载多个图片作为zip,但它不起作用:(

我读过这个:Symfony2 : How to force download multiple images in one zip file with Ajax

但同样的问题,没有任何反应:(

这是我的代码,首先是js函数:

function downloadZip() {
var searchIDs = $( ".picDashboardBox:checked" ).map( function( ) {
    return $(this).parent( ).parent( ).prev('td').prev('td').children( ).children( ).children( ).children( ).attr( 'src' );
} ).get( );
$.ajax(
        {
            type: 'POST',
            url: $( '#download' ).attr( 'url' ),
            data: { searchIDs: searchIDs },
            success: function(response) {
                return response;
            }
        }
    );

}

这是我的控制器:

public function downloadPicturesAction( )
{
    $request = $this->get( 'request' );
    $pictures = $request->request->get( 'searchIDs' ) == null ? array( ) : $request->request->get( 'searchIDs' );

    $zip = new ZipArchive( );
    $filename = 'pictures.zip';
    if ( $zip->open( "/tmp/$filename", \ZIPARCHIVE::CREATE | \ZIPARCHIVE::OVERWRITE ) === TRUE )
    {
        foreach ( $pictures as $picture )
        {
            $tmp = explode( '/', $picture );
            $zip->addFromString( $tmp[count( $tmp ) - 1], file_get_contents( $picture ) );
        }
        $zip->close();
    }
    else
    {
        error_log( "can't create zip file to download" );
    }

    $response = new Response( );

    $response->headers->set( "Content-type", mime_content_type( "/tmp/$filename" ) );
    $response->headers->set( "Content-Disposition", "attachment; filename=$filename" );
    $response->headers->set( "Content-length", filesize( "/tmp/$filename" ) );
    $response->headers->set( "Pragma", "no-cache" );
    $response->headers->set( "Expires", "0" );
    $response->send( );

    $response->setContent( readfile( "/tmp/$filename" ) );

    return $response;
}

我在纯php中尝试了来自控制器的代码,它有效,我可以下载zip,所以我不知道这里有什么问题(我对JS很不好,所以也许有些问题^^)< / p>

Thx:)

ARVI

0 个答案:

没有答案