php - 如何在 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);
}
});
}
});
});
解决方案
$directory_path
should be an absolute path, for example, public_path('/uploads/products/')
, or $zip->addFile
cannot read the file's content.
推荐阅读
- r - 使用几个月的几个最大值计算年平均值
- entity-framework - VScode 中的 EF 迁移
- flutter - 如何在小部件测试中测试特定的 ProviderNotFoundException
- r - 我可以让 dplyr 的连接函数对意外的重复变量抛出错误吗?
- javascript - 在 useEffect 中使用“array-contains”的 react-native-firebase 查询不断刷新组件
- javascript - 点击它时如何增加价值?
- unity3d - Hololens 2 Gaze 光标不执行按钮
- xcode - dyld:库未加载:@rpath/OneSignal.framework/OneSignal
- c# - 在 ASP.Net Core 中压缩之前记录响应正文
- css - 以特定形式制作透明区域