BinaryFileResponse in Laravel undefined

前端 未结 2 378
我寻月下人不归
我寻月下人不归 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条回答
  •  萌比男神i
    2020-12-29 21:33

    You may want to exclude headers or set different headers for file download requests by checking the header method exists in the returned Closure.

    File download requests typically have the header method omitted from Closure.

    
    public function handle($request, Closure $next)
    {
        $handle = $next($request);
    
        if(method_exists($handle, 'header'))
        {
            $handle->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');
        }
    
        return $handle;
        
    }
    

    If you need to set headers for a file request (as other answer suggested) $handle->headers->set() can be used in an else condition:

    
    public function handle($request, Closure $next)
    {
        $handle = $next($request);
    
        if(method_exists($handle, 'header'))
        {
            // Standard HTTP request.
    
            $handle->header('Access-Control-Allow-Origin' , '*');
    
            return $handle;
        }
    
        // Download Request?
    
        $handle->headers->set('Some-Other-Header' , 'value')
    
        return $handle;
        
    }
    

提交回复
热议问题