Laravel 5 返回响应()->;下载();显示空白页

Laravel 5 返回响应()->;下载();显示空白页,laravel-5,Laravel 5,我有这个密码 return response()->download(storage_path('app/files/gggusers.xlsx')); 在我的控制器里。它执行时没有任何问题,但不会触发浏览器下载excel文件,而是显示一个空白的白页。我确信文件名和位置是正确的,因为如果我只是将文件名gggusers.xlsx更改为其他名称或删除该文件,Laravel将显示此错误 文件“D:\web\speak\storage\app/files/gggusers.xlsx”不存在 re

我有这个密码

return response()->download(storage_path('app/files/gggusers.xlsx'));
在我的控制器里。它执行时没有任何问题,但不会触发浏览器下载excel文件,而是显示一个空白的白页。我确信文件名和位置是正确的,因为如果我只是将文件名gggusers.xlsx更改为其他名称或删除该文件,Laravel将显示此错误


文件“D:\web\speak\storage\app/files/gggusers.xlsx”不存在

return response()->download(storage_path('app/files/gggusers.xlsx'));
在另一个函数中,从route中加载的函数调用它,如下所示:

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index(){
        $this->download();
    }

    public function download(){
        return response()->download(storage_path('app/files/gggusers.xlsx'));
    }
}
上面的代码将显示一个空白页。没有错误。调用函数download没有任何问题,但不知何故,之后它只显示空白页

public function download(){
        return response()->download(storage_path('app/files/gggusers.xlsx'));
}
如果我把代码

return response()->download(storage_path('app/files/gggusers.xlsx'));
函数索引()
中,将下载该文件


如果有人能向我解释原因,我会非常感激。这是某种bug还是PHP/Laravel的预期行为。因为这个问题浪费了几个小时。

我知道这是一个老问题,但我刚刚遇到了同样的问题并解决了它。您的问题是您将响应(文件)从<代码>下载()/代码>返回到调用函数,<代码>索引()/<代码>,但是您没有从<代码>索引()/代码>返回到浏览器选项卡,因此返回空白页。

public function download(){
        return response()->download(storage_path('app/files/gggusers.xlsx'));
}
在这里,您将下载文件返回到
index()
函数,但在
index()
函数中,您不会将任何内容返回到浏览器。因此,改变这一点:

public function index(){
        $this->download();
}
为此:

public function index(){
        return $this->download();
}
将按预期将文件返回到浏览器选项卡


这是正确的方法。

您需要传递相对路径,请先尝试从您的url访问它好吗?