Laravel 7中未找到文件异常

时间:2020-11-10 15:01:18

标签: laravel download

我正在开发一个laravel应用程序,我想通过单击一个按钮下载文件。我已将文件存储在/storage/app/public/1.pdf中,并创建了指向public文件夹的符号链接。现在,我尝试了多种下载文件的方法,但是每次都会收到错误File Not Fount Exception at path

我尝试了以下方式下载文件:

1 -
$name = "file.pdf";
$file = storage_path(). "/app/public/1.pdf";
$headers = array(
'Content-Type: application/pdf');
return Storage::download($file, $name, $headers);

2 - 
$name = "file.pdf";
    $file = public_path(). "/storage/1.pdf";
    $headers = array(
          'Content-Type: application/pdf',
        );

    return Storage::download($file, $name, $headers);


3 - 
$name = "file.pdf";
    $file = Storage::url("1.pdf");
    $headers = array(
          'Content-Type: application/pdf',
        );

    return Storage::download($file, $name, $headers);  

这是我收到的错误消息:

enter image description here

enter image description here

我尝试了很多次,但对我没有任何帮助。如有任何帮助,我们将不胜感激。

1 个答案:

答案 0 :(得分:1)

来自文档:
The Local Driver

使用local驱动程序时,所有文件操作都相对于root配置文件中定义的filesystems目录。默认情况下,此值设置为storage/app目录。

因此,如果文件存储在storage/app/public/1.pdf中,请不要将这样的绝对路径传递到Storage Facade:

$file = storage_path(). "/app/public/1.pdf";
return Storage::download($file);

请使用相对于root配置文件中定义的filesystems目录的相对路径:

$file = "public/1.pdf";
$name = "file.pdf";
return Storage::download($file, $name);
相关问题