首页 > 解决方案 > 如何在 laravel 5.4 中从数据库中下载多个图像

问题描述

在我的laravel 5.4应用程序中,我想从数据库中下载所选产品的多个图像,它正在其中创建多个 zip 文件,public_path('uploads/download-images');但不下载文件,我该如何下载它。

下载这个的最好方法是什么?

我究竟做错了什么 ?有人可以帮忙吗?预先感谢。

以下是我的代码。

public function downloads_all(Request $request) 
 {
    $public_dir = '';
    $$zipFileName = '';
    $zip = '';
    $product_images =  DB::table('product_images')
                ->where('deleted_at', null)
                ->whereIn('product_id',  explode(',', $request->ids))
                ->get();
    if ($product_images) 
    {
        foreach ($product_images as $value) 
        {
            $zipFileName = 'product_' . $value->product_id . '_images.zip';
            $zip = new ZipArchive;
            $public_dir = public_path('uploads/download-images');
            if (!is_dir($public_dir)) 
            {
                mkdir($public_dir, 0777, true);
            }
            if ($zip->open($public_dir . '/' . $zipFileName, ZipArchive::CREATE) === TRUE) 
            {
                foreach ($product_images as $image) 
                {
                    $directory_path = base_path() . '/public/uploads/products/' . $image->product_id . '/images/';
                    $image_path = $image->image_path;
                    $file_name = basename($image_path);
                    $zip->addFile($directory_path . $file_name, $file_name);
                }
                $zip->close();
            }
        }

        $files = File::files(public_path('uploads/download-images'));
        $filecount = count($files);
        $filetopath = array();
        for ($i=0; $i < $filecount; $i++) 
        { 
            $filetopath = $public_dir . '/' . $zipFileName;
            $headers = array(
                'Content-Type' => 'application/octet-stream',
                'Expires' => 0,
                'Content-Transfer-Encoding' => 'binary',
                'Content-Length' => filesize($filetopath),
                'Cache-Control' => 'private, no-transform, no-store, must-revalidate',
                'Content-Disposition' => 'attachment; filename="' . $zipFileName . '"',
            );

            if (file_exists($filetopath)) 
            {
                return response()->download($filetopath, $zipFileName, $headers)->deleteFileAfterSend(true);
            } 
            else 
            {
                return ['status'=>'zip file does not exist'];
            }
        }
    }
    else
    {
        return back();
    }
}

这是js

$(document).ready(function () 
{
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    $('#download-master').on('click', function(e) 
    {
        if($(this).is(':checked',true))  
        {
            $(".multiple_download").prop('checked', true);  
        } else {  
            $(".multiple_download").prop('checked',false);  
        }  
    });

    $('.download-all').on('click', function(e) {

        var allVals = [];  
        $(".multiple_download:checked").each(function() {  
            allVals.push($(this).attr('data-id'));
        });  

        if(allVals.length <=0)  
        {  
            alert("Please select row to download.");  
        }  
        else 
        {  
            var join_selected_values = allVals.join(",");
            $.ajax({
                url: $(this).data('url'),
                type: 'POST',
                data:'_token = <?php echo csrf_token() ?>',
                data: 'ids='+join_selected_values,
                success: function (data) 
                {
                    alert('Seccussfully Downloaded');
                },
                error: function (data) {
                    alert(data.responseText);
                }
            }); 
        }  
    });
});

标签: phplaravel

解决方案


$directory_path should be an absolute path, for example, public_path('/uploads/products/'), or $zip->addFile cannot read the file's content.


推荐阅读