【发布时间】:2020-03-12 23:31:01
【问题描述】:
我正在使用 laravel 并尝试下载文件并更改文件名。 文档告诉我,我可以使用第二个参数作为新文件名,但文件名不会以某种方式更改为“testdownload.zip”。我做错了什么?
https://laravel.com/docs/5.8/responses#file-downloads
// routes
Route::group(['middleware' => ['web']], function()
{
Route::group(['namespace' => 'App\Http\Controllers'], function()
{
Route::group(['middleware' => ['role:root|admin|store|user']],
function()
{
// download purchase
Route::get('dashboard/order/downloadfile', [
'as' => 'order-download',
'uses' => 'Post\PostController@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']) ) 返回以下内容(可能是 perms / 权限 0100666?我在 Windows 上):
BinaryFileResponse {#449 ▼
#file: File {#464 ▼
path: "C:\xampp71\htdocs\storage\app"
filename: "lorem-ipsum-shop-1.zip"
basename: "lorem-ipsum-shop-1.zip"
pathname: "C:\xampp71\htdocs\storage\app/lorem-ipsum-shop-1.zip"
extension: "zip"
realPath: "C:\xampp71\htdocs\storage\app\lorem-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:\xampp71\htdocs\storage\app\lorem-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
}
【问题讨论】:
-
为什么这被否决了?
-
dd(response()->download(storage_path('app/lorem-ipsum-shop-1.zip'), 'testdownload.zip', ['Content-Type: application/zip' ]));;请检查。
-
所以你想在下载文件时更改文件名对吗>?
-
@VikasKatariya 下载的文件名应该与服务器上的原始文件名不同
标签: laravel downloadfile