gpt4 book ai didi

Laravel 创建下载链接到存储

转载 作者:行者123 更新时间:2023-12-04 17:48:35 25 4
gpt4 key购买 nike

我在 /storage/excel/exports/abc.xls 中有一个文件,我想为它创建一个下载链接,所以我写道:

<a href="{{asset('storage/excel/exports/'.$file)}}" download>{{$file}}</a>

但是当我点击链接时,它找不到文件。然后我查看了 Inspect Element 中的 href,链接是 http://localhost/[my-project]/public/storage/excel/exports/abc.xls/p>

当我使用 Inspect Element 清理 public/ 并更改 href 值时,链接工作正常并下载了文件。

我有不同的方法来解决这个问题,但我确信它们都不是最佳做法。

在 laravel 的存储文件夹中创建文件链接的最佳方法是什么?

最佳答案

如果你使用的是 Laravel 5.5,你可以使用 Storage类(class)。然后,您可以使用 Storage::url($file),它应该会为您的文件获取正确的 URL。

但是,根据文档:

Remember, if you are using the local driver, all files that should be publicly accessible should be placed in the storage/app/public directory. Furthermore, you should create a symbolic link at public/storage which points to the storage/app/public directory.

关于Laravel 创建下载链接到存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46982456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com