1

这是我的 AJAX 请求:

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,获取正确的图像路径:

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;
}

问题:这是获取二进制数据,特殊字符而不是图像。
图像路径是属性。

4

1 回答 1

2

你添加使用了吗

use ZipArchive;

在你的控制器中,也试试这个

$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

于 2013-09-24T17:34:07.637 回答