laravel下载响应文件名未更改
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了laravel下载响应文件名未更改相关的知识,希望对你有一定的参考价值。
我正在使用laravel并尝试下载文件并更改文件名。文档告诉我可以将第二个参数用作新文件名,但是文件名不会以某种方式更改为“ testdownload.zip”。我在做什么错?
https://laravel.com/docs/5.8/responses#file-downloads
// routes
Route::group(['middleware' => ['web']], function()
{
Route::group(['namespace' => 'AppHttpControllers'], function()
{
Route::group(['middleware' => ['role:root|admin|store|user']],
function()
{
// download purchase
Route::get('dashboard/order/downloadfile', [
'as' => 'order-download',
'uses' => 'PostPostController@download'
]);
});
}
}
// method in Controller
public function download(Request $request)
{
return response()->download(storage_path('app/lorem-ipsum-shop-1.zip'), 'testdownload.zip', ['Content-Type: application/zip']);
}
当我做dd(response()-> download(storage_path('app / lorem-ipsum-shop-1.zip'),'testdownload.zip',['Content-Type:application / zip'])时))返回以下内容(可能是权限/权限0100666吗?我在Windows上):
BinaryFileResponse {#449 ▼
#file: File {#464 ▼
path: "C:xampp71htdocsstorageapp"
filename: "lorem-ipsum-shop-1.zip"
basename: "lorem-ipsum-shop-1.zip"
pathname: "C:xampp71htdocsstorageapp/lorem-ipsum-shop-1.zip"
extension: "zip"
realPath: "C:xampp71htdocsstorageapplorem-ipsum-shop-1.zip"
aTime: 2019-11-14 11:34:52
mTime: 2019-11-14 11:34:52
cTime: 2019-11-01 16:05:10
inode: 0
size: 24626
perms: 0100666
owner: 0
group: 0
type: "file"
writable: true
readable: true
executable: false
file: true
dir: false
link: false
linkTarget: "C:xampp71htdocsstorageapplorem-ipsum-shop-1.zip"
}
#offset: 0
#maxlen: -1
#deleteFileAfterSend: false
+headers: ResponseHeaderBag {#234 ▼
#computedCacheControl: array:1 [▼
"public" => true
]
#cookies: []
#headerNames: array:5 [▼
0 => 0
"cache-control" => "Cache-Control"
"date" => "Date"
"last-modified" => "Last-Modified"
"content-disposition" => "Content-Disposition"
]
#headers: array:5 [▼
0 => array:1 [▶]
"cache-control" => array:1 [▶]
"date" => array:1 [▶]
"last-modified" => array:1 [▶]
"content-disposition" => array:1 [▼
0 => "attachment; filename=testdownload.zip"
]
]
#cacheControl: array:1 [▼
"public" => true
]
}
#content: null
#version: "1.0"
#statusCode: 200
#statusText: "OK"
#charset: null
}
答案
使用您提供的代码,我无法重现该错误。我实际上确实得到了一个testdownload.zip
文件。因此,我认为您的应用程序的其他部分正在干扰。
- 您是否有任何中间件可以调整响应(或相应的响应头)?
- 您如何称呼此端点?直接从浏览器?通过AJAX吗?
- 您能否显示完整的控制器和
web.php
的相关部分 - 您可以使用chrome调试工具来验证响应标头吗?它们的外观应类似于下图。请注意
Content-Disposition
标头。
另一答案
总是清除缓存...我认为它在重命名之前会存储文件名。
以上是关于laravel下载响应文件名未更改的主要内容,如果未能解决你的问题,请参考以下文章
更改语言环境后未调用 onConfigurationChange