BinaryFileResponse in Laravel undefined

前端 未结 2 390
我寻月下人不归
我寻月下人不归 2020-12-29 20:39

I have got the following problem: I want to return an Image on the route /getImage/{id} The function looks like this:

public function getImage($id){
   $imag         


        
2条回答
  •  余生分开走
    2020-12-29 21:10

    The problem is that you're calling ->header() on a Response object that doesn't have that function (the Symfony\Component\HttpFoundation\BinaryFileResponse class). The ->header() function is part of a trait that is used by Laravel's Response class, not the base Symfony Response.

    Fortunately, you have access to the headers property, so you can do this:

    $response = $next($request);
    
    $response->headers->set('Access-Control-Allow-Origin' , '*');
    $response->headers->set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE');
    $response->headers->set('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization, X-Requested-With, Application');
    
    return $response;
    

提交回复
热议问题