【发布时间】:2022-03-24 14:06:27
【问题描述】:
我遇到了以下问题: 我想在路线 /getImage/{id} 上返回一个图像 函数如下所示:
public function getImage($id){
$image = Image::find($id);
return response()->download('/srv/www/example.com/api/public/images/'.$image->filename);
}
当我这样做时,它会返回给我:
FatalErrorException in HandleCors.php line 18:
Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::header()
我在控制器的开头有use Response;。
我不认为 HandleCors.php 是问题,但无论如何:
<?php namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Routing\Middleware;
use Illuminate\Http\Response;
class CORS implements Middleware {
public function handle($request, Closure $next)
{
return $next($request)->header('Access-Control-Allow-Origin' , '*')
->header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE')
->header('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization, X-Requested-With, Application');
}
}
我实际上不知道为什么会发生这种情况,因为它与 Laravel 文档中描述的完全一样。 当我收到错误时,我已经更新了 Laravel,但这并没有解决它。
【问题讨论】:
-
你使用的是哪个版本的 Laravel?
-
版本 5.0.21 更新后 5.0.14 之前
-
你应该使用
return response()->download(public_path("images/'.$image->filename"));或类似的东西。 -
好吧,错误似乎很正确:您不能设置 $request 的标头,只能设置 Response..
-
还有什么解决办法,因为我现在不知道该怎么办?