【问题标题】:Response::download error because of header in laravelResponse::download 错误,因为 laravel 中的标头
【发布时间】:2016-04-24 14:12:15
【问题描述】:

控制器:

public function download(){

 $headers = array('Content-Type' => 'text/csv');
 return Response::download('download.csv', 'download.csv', $headers)->setContentDisposition('inline');

}

过滤器:

 $response->header('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0');

错误:

 [2016-01-19 15:34:29] local.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::header()' in C:\xampp\htdocs\development\app\filters.php:60
  Stack trace:
  #0 [internal function]: Illuminate\Exception\Handler->handleShutdown()
  #1 {main} [] []

我不知道如何解决这种问题。我已经尝试过this,但还是不行。

【问题讨论】:

  • 你为什么要标记两个 Laravel 版本?我假设您使用的是 Laravel 4,因为您已指定使用过滤器?
  • @MikeRockett 是的,很抱歉。也许有人可以在 Laravel 5 的标题方面帮助我。

标签: php laravel-4


【解决方案1】:

https://github.com/palanik/lumen-cors/issues/9

/**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next) {

        if ($request->isMethod('OPTIONS')) {
            $response = new Response("", 200);
        }
        else {
            $response = $next($request);
        }

        // Added this to fix the image download problem
        // Only set Cors if its not een image
        if(strpos($response->headers->get('content-type'), 'image') === false) {
          $this->setCorsHeaders($request, $response);
        }

        return $response;
    }

【讨论】:

    猜你喜欢
    • 2015-05-31
    • 2015-11-17
    • 2013-05-28
    • 2013-12-23
    • 2016-01-02
    • 1970-01-01
    • 2014-10-24
    • 2018-11-17
    • 1970-01-01
    相关资源
    最近更新 更多