我正在开发一个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);
这是我收到的错误消息:
我尝试了很多次,但对我没有任何帮助。如有任何帮助,我们将不胜感激。
答案 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);