Symfony2的:如何强制下载多个图像在一个zip文件中使用Ajax [英] Symfony2 : How to force download multiple images in one zip file with Ajax
本文介绍了Symfony2的:如何强制下载多个图像在一个zip文件中使用Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的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;
}
这是我的控制器的方法,我得到的产品ID,并从数据库中获取这些ID的,得到适当的图像路径:
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;
}
问题:这是越来越二进制数据,特殊字符,而不是图像
。
图片路径propers。
Problem : that's getting binary data, special character not images.
Images paths are propers.
推荐答案
也û添加使用
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));
如果仍严格与物质再尝试删除sendHeaders
if still strict with matter then try to remove the sendHeaders
这篇关于Symfony2的:如何强制下载多个图像在一个zip文件中使用Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文