php - Symfony2 : How to force download multiple images in one zip file with Ajax -
this 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 controller method, i'm getting product ids , fetch these id's database, getting proper images paths :
public function zipmultipledownload($data){ foreach($data $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 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 $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 $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 headers foce download 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 propers.
did u add use
use ziparchive;
in controller , try 1
$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 $response->sendheaders(); $response->setcontent(file_get_contents($filename));
if still strict matter try remove sendheaders
Comments
Post a Comment