实际上,我正在尝试使用以下方法发送包含文件响应(用于在客户端下载状态栏包)的内容长度标题:
return response()->header('Content-Length', $fileSize)->file($this->filesPath.$fileName);
但我发现了一个错误:
BadMethodCallException: Method Illuminate\Routing\ResponseFactory::header does not exist. in file /www/vendor/laravel/framework/src/Illuminate/Macroable/Traits/Macroable.php on line 113
当响应中有"file()“时,似乎无法识别该方法。
以下是我的响应头:
注意:基于Linux的Apache 8.1服务器运行Laravel 8和PHP8.1
辅助信息:
在不同的环境和多个文件上进行测试之后,我发现Content-Length
头只处理图像。我在Laravel 9&8中测试了它,但仍然不能手动发送Content-Length
头。任何其他标头都正常工作。
发布于 2022-09-09 08:43:04
使用预构建的download()
方法发送标头。
$filesPath = 'path/to/file'; // change this
$fileName = 'file-name'; // change this
$fileSize = 'size'; // change this
$headers = [
'Content-Length' => $fileSize
];
return response()->download($filesPath, $fileName, $headers);
https://stackoverflow.com/questions/73658802
复制相似问题