php - 强制浏览器下载通过 PHP/Laravel 中的外部 URL 检索到的文件
问题描述
我已经坚持了几个小时。也许我的心已经累了。我希望有人可以帮助我。我正在开发一个连接到创建和编辑发票的外部应用程序的 Laravel 应用程序。我的一条路线应该允许用户从此外部应用程序下载 PDF 发票。我的控制器类似于下面的代码:
public function download(Invoice $invoice)
{
// Creates an instance of the remote invoice
$remoteInvoiceFile = RemoteInvoiceSoftware::find($invoice->id);
return response()->streamDownload(function () use ($remoteInvoiceFile ) {
// This calls the remote server and the server responds with the PDF file contents
$file = $remoteInvoiceFile->download();
echo $file;
}, 'file.pdf');
}
我streamDownoad
从 Laravel 文档中阅读了有关该功能的信息,并以与显示相同的方式实现了它。但是我在可以下载 PDF 文件时遇到错误,但是,不仅文件小于 5KB(原始发票文件约为 60KB),当我尝试打开它时也会出现错误。关于文件损坏或解析不正确的一些事情。
当我echo $remoteInvoiceFile->download()
不使用时,streamDownload
我会得到这样的结果:
请帮我弄清楚发生了什么以及如何解决这个问题。谢谢!!
解决方案
推荐阅读
- kotlin - Kotlin 协程 GlobalScope.launch 与 runBlocking
- javascript - 从子组件中状态的数组中删除项目
- recursion - 计算划分问题的大 O 复杂度
- ruby-on-rails - Rails lightbox2 ActionController::RoutingError
- bash - 未使用 .bash_profile 中定义的 xargs 调用函数
- python - 由于 pytz 包失败,无法使用 pipenv 安装 Django
- html - CSS中RGB和RGBA规则的区别(都接受Alpha值)?
- javascript - 自动完成匹配单词中任何地方的模式,它只能在开头匹配吗?
- php - PHP - 调用内部 API
- c# - DataVisualization.Charting.Chart PostPaint 事件过早触发