Laravel:download()不会触发文件保存对话框

发布于 2025-01-11 23:02:36 字数 866 浏览 0 评论 0原文

我的 laravel(8.0) 测试代码 (web.php):

Route::get(
  '/dddd',
  fn () =>
  response()->download(public_path('version.pdf'), null, ['Content-Type' => 'application/pdf'])
);

此代码在我的宅基地测试环境中运行得很好。浏览器将弹出一个文件对话框并要求用户保存文件。

但是,当我将此应用程序放入 docker 中时(图片:php:8.0-apache-buster),弹出对话框将不会显示,浏览器会直接使用二进制字符打开文件。我尝试在 chrome 中捕获响应,响应标头如下所示:

Request URL: http://redtrain.test:8080/dddd
Request Method: GET
Status Code: 200 OK
Remote Address: 192.168.10.10:8080
Referrer Policy: strict-origin-when-cross-origin
Connection: Keep-Alive
Content-Encoding: gzip
Content-Length: 1305
Content-Type: text/html; charset=UTF-8
Date: Fri, 04 Mar 2022 05:39:56 GMT
Keep-Alive: timeout=5, max=100
Server: Apache/2.4.38 (Debian)
Vary: Accept-Encoding
X-Powered-By: PHP/8.0.14

我注意到标头与我的代码不一致,也许它是由 Apache 更新的?有人可以给我一些提示吗?

My laravel(8.0) test code (web.php):

Route::get(
  '/dddd',
  fn () =>
  response()->download(public_path('version.pdf'), null, ['Content-Type' => 'application/pdf'])
);

This code works perfert in my homestead test environment. The browser will popup a file dialog and ask user to save the file.

But when I put this application inside docker (image: php:8.0-apache-buster), the popup dialog will not show and the browser open the file directly with binary characters. I tried to catch the response in chrome, the response headers is like following:

Request URL: http://redtrain.test:8080/dddd
Request Method: GET
Status Code: 200 OK
Remote Address: 192.168.10.10:8080
Referrer Policy: strict-origin-when-cross-origin
Connection: Keep-Alive
Content-Encoding: gzip
Content-Length: 1305
Content-Type: text/html; charset=UTF-8
Date: Fri, 04 Mar 2022 05:39:56 GMT
Keep-Alive: timeout=5, max=100
Server: Apache/2.4.38 (Debian)
Vary: Accept-Encoding
X-Powered-By: PHP/8.0.14

I noticed that header is not consistent with my code, maybe it is updated by Apache? Can someone give me some hint?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文