如何使用@apiDescription 实现批量文件下载操作?

更新于
2026-09-26 10:16:14
1阅读来源:SEO资源
  • 内容介绍
  • 相关推荐

本文共计215个文字,预计阅读时间需要1分钟。

如何使用@apiDescription 实现批量文件下载操作?

简化后的代码开头及内容:

phppublic function download(){ $user=\Auth::user(); $request=array_filter($this->request->input()); array_walk($request, function ($value, $key) { if (is_array($value)) { // 处理数组值 } });}

附加图片的批量下载

1.[代码][PHP]代码

public function download() { $user = \Auth::user(); //验证字段 $request = array_filter($this->request->input()); array_walk($request, function (&$value, $key) { if (is_array($value)) { $value = array_filter($value); } }); $validator = \Validator::make($request, [ 'attachment_ids' => 'required|array|min:1', ]); if ($validator->fails()) { return $this->errorBadRequest($validator->messages()->all()); } $attachmentIds = $this->request->get('attachment_ids'); $attachments = Attachment::whereIn('id', $attachmentIds)->get(); if (!$attachments->count()) { return $this->response->errorNotFound(); } if ($attachments->count() == 1) { $attachment = $attachments->first(); if (!$attachment->allowDownload($user)) { return $this->response->errorForbidden(); } return response()->download(public_path($attachment->relative_path)); } else { $zip = new \ZipArchive(); $zipFile = storage_path('app/cache/' . date('Ymdhis') . uniqid() . '.zip'); $zip->open($zipFile, \ZIPARCHIVE::CREATE); foreach ($attachments as $attachment) { if (!$attachment->allowDownload($user)) { continue; } $filename = basename($attachment->relative_path); $zip->addFile($attachment->relative_path, $filename); } $zip->close(); return response()->download($zipFile); } }

如何使用@apiDescription 实现批量文件下载操作?

本文共计215个文字,预计阅读时间需要1分钟。

如何使用@apiDescription 实现批量文件下载操作?

简化后的代码开头及内容:

phppublic function download(){ $user=\Auth::user(); $request=array_filter($this->request->input()); array_walk($request, function ($value, $key) { if (is_array($value)) { // 处理数组值 } });}

附加图片的批量下载

1.[代码][PHP]代码

public function download() { $user = \Auth::user(); //验证字段 $request = array_filter($this->request->input()); array_walk($request, function (&$value, $key) { if (is_array($value)) { $value = array_filter($value); } }); $validator = \Validator::make($request, [ 'attachment_ids' => 'required|array|min:1', ]); if ($validator->fails()) { return $this->errorBadRequest($validator->messages()->all()); } $attachmentIds = $this->request->get('attachment_ids'); $attachments = Attachment::whereIn('id', $attachmentIds)->get(); if (!$attachments->count()) { return $this->response->errorNotFound(); } if ($attachments->count() == 1) { $attachment = $attachments->first(); if (!$attachment->allowDownload($user)) { return $this->response->errorForbidden(); } return response()->download(public_path($attachment->relative_path)); } else { $zip = new \ZipArchive(); $zipFile = storage_path('app/cache/' . date('Ymdhis') . uniqid() . '.zip'); $zip->open($zipFile, \ZIPARCHIVE::CREATE); foreach ($attachments as $attachment) { if (!$attachment->allowDownload($user)) { continue; } $filename = basename($attachment->relative_path); $zip->addFile($attachment->relative_path, $filename); } $zip->close(); return response()->download($zipFile); } }

如何使用@apiDescription 实现批量文件下载操作?