首页 > 解决方案 > Laravel Storage::从Gmail url下载不起作用

问题描述

在我的 Laravel 5.8 应用程序中,我动态生成 url 以下载存储在storage目录下的文档。

我通过电子邮件发送生成的 url,因此用户可以单击它以下载文件。

在浏览器中直接引入接收到的链接时,生成的文件被正确下载。但是,从 gmail 邮箱中单击它时,会打开一个新选项卡,并在一秒钟后关闭而不下载文件。

路由web.php文件:

Route::get('download/{path}', ['as' => 'downloadStorage', 'uses' => 'AppController@downloadStorage'])
  ->where('path', '.*')->middleware('auth');

我的控制器代码:

public function downloadStorage($path)
{
    $user = \Auth::user();

    // Here I have some code that verify that user is logged
    // and has permissions to download this file
    $hasPermission = true;

    if ($hasPermission) {
        return Storage::download($path);  // <-- THIS LINE IS REACHED CORRECTLY
    } else {
        return abort(403);
    }
}

知道如何解决这个问题吗?

我的猜测是,在将文件提供给浏览器之前,新选项卡已关闭(由于return),但我无法找到任何解决方案。

标签: laravelgmaillaravel-storage

解决方案


我不确定为什么应用程序无法提供文件以供下载,但是我找到了以下解决方案:

而不是为downliading提供文件:

return Storage::download($path);

我将其退回,以便在新选项卡中打开它:

return \Response::file(Storage::path($path));

推荐阅读