我正在尝试使用此方法下载多个 PDF:
public function download(){
$documents = Documents::all();
foreach ($documents as $document) {
response()->download(public_path("/uploads/documents/".$document->filename));
}
return 1;
}
这可行,但只能下载一个文件。其他文件没有下载。为什么?
我找到了一种使用 Laravel 下载 zip 文件夹中多个文件的方法,步骤如下:
下载包:composer 需要 stechstudio/laravel-zipstream
添加命名空间:使用STS\ZipStream\Facades\Zip;
代码片段:
$invoicesArray = [];
foreach ($InvoiceNumbers as $key => $invoiceNumber) {
$invoicesArray[] = storage_path("app/invoice-attachments/Invoice_" . $invoiceNumber . ".pdf");
}
return Zip::create("Invoices.zip", $invoicesArray);
Zip::create() 首先创建一个 zip 文件夹,invoices 数组中的所有文件都将被压缩到该文件夹中。 有关更多详细信息,请查找此包的官方 git 存储库: https://github.com/stechstudio/laravel-zipstream