user2812049
user2812049

Reputation: 31

Symfony2 : How to force download multiple images in one zip file with Ajax

This is my AJAX request :

function DownloadZip() {

        var searchIDs = $("#image_id:checked").map(function(){
            return $(this).val();
        }).get(); 

        $.ajax(
            {
                type:'POST',
                url: $('#admin_product_zip').text(),
                data: {searchIDs:searchIDs},        
                success: function(response) {
                }
            }
        );
        return false;
        }

And this is my controller method, I'm getting the product ids and fetch these id's from database, getting the proper images paths :

public function zipMultipleDownload($data){

    foreach($data as $product_ind) {
        $product = $this->repo->find(3);
        $images=$product->getColorImagesPaths();
        $itemImages=$product->getItemImagesPaths();
        $archive_file_name =$product->getName().$product->getId().'.zip';
        //print_r($itemImages);
        //return new response('test');
    } 
    $this->zipFilesAndDownload($images,$archive_file_name,$itemImages);
    //return json_encode($itemImages);

}
// Function for dowloading images in zip format
public function zipFilesAndDownload($file_names,$archive_file_name,$itemImages)
{
    $zip = new ZipArchive();

    if ($zip->open($archive_file_name, \ZIPARCHIVE::CREATE | \ZIPARCHIVE::OVERWRITE) === TRUE)
    {
        foreach ($file_names as $files) {
        // $zip->addFile("uploads/ltf/products/display/web/5232f56d0291a.png","uploads/ltf/products/display/web/5232f56d0291a.png");
            $zip->addFile($files['web'], $files['web']);
            $zip->addFile($files['iphone'], $files['iphone']);
        }  
        foreach ($itemImages as $itemfiles) {
            $zip->addFile($itemfiles['web'],$itemfiles['web']);
            $zip->addFile($itemfiles['iphone4s'], $itemfiles['iphone4s']);
            $zip->addFile($itemfiles['iphone5'], $itemfiles['iphone5']);
       }
       $zip->close();
     }
     else {
         return "can not open";
     }

     $response =new Response();
     //then send the headers to foce download the zip file
     $response->headers->set('Content-Type','application/zip');
     $response->headers->set('Content-Disposition', 'attachment; filename="' . basename($archive_file_name) . '"');        
     $response->headers->set('Pragma', "no-cache");
     $response->headers->set('Expires', "0");
     $response->headers->set('Content-Transfer-Encoding', "binary");
     $response->sendHeaders();
     $response->setContent(readfile($archive_file_name));
     return $response;
}

Problem : that's getting binary data, special character not images.
Images paths are propers.

Upvotes: 1

Views: 3315

Answers (1)

Suresh Kumar Amrani
Suresh Kumar Amrani

Reputation: 935

did u add use

use ZipArchive;

in your controller and also try this one

$response = new Response();

// Set headers

$response->headers->set('Cache-Control', 'private');
$response->headers->set('Content-type', mime_content_type($filename));
$response->headers->set('Content-Disposition', 'attachment; filename="' . basename($filename) . '"');
$response->headers->set('Content-length', filesize($filename));

// Send headers before outputting anything
$response->sendHeaders();

$response->setContent(file_get_contents($filename));

if still strict with matter then try to remove the sendHeaders

Upvotes: 2

Related Questions