在 Laravel 中下载文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50907385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Downloading a file in Laravel
提问by Jesse Orange
In Laravel, I have a page in which I am trying to provide download links to files and I am using Laravel Filemananager to upload these files.
在 Laravel 中,我有一个页面,我试图在其中提供文件的下载链接,并且我正在使用 Laravel Filemananager 上传这些文件。
The path to these files is set in the lfm.php
config file as:
这些文件的路径在lfm.php
配置文件中设置为:
/*
|--------------------------------------------------------------------------
| Working Directory
|--------------------------------------------------------------------------
*/
// Which folder to store files in project, fill in 'public', 'resources', 'storage' and so on.
// You should create routes to serve images if it is not set to public.
'base_directory' => 'public',
'images_folder_name' => 'assets/uploads/images',
'files_folder_name' => 'storage/files',
'shared_folder_name' => 'shares',
'thumb_folder_name' => 'thumbs',
I followed the documentation on the Laravel website regarding the public
and storage
folders which says the following:
我遵循了 Laravel 网站上关于public
和storage
文件夹的文档,其中说明如下:
The public disk is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores these files in storage/app/public. To make them accessible from the web, you should create a symbolic linkfrom public/storageto storage/app/public.
This convention will keep your publicly accessible files in one directory that can be easily shared across deployments when using zero down-time deployment systems like Envoyer.
To create the symbolic link, you may use the storage:link Artisan command:
php artisan storage:link
公共磁盘用于将要公开访问的文件。默认情况下,公共磁盘使用本地驱动程序并将这些文件存储在 storage/app/public 中。为了使它们可以从网络访问,您应该创建一个从public/storage到 storage/app/public的符号链接。
该约定将您的可公开访问的文件保存在一个目录中,当使用 Envoyer 等零停机时间部署系统时,该目录可以轻松地在部署之间共享。
要创建符号链接,您可以使用 storage:link Artisan 命令:
php工匠存储:链接
Before anything else I also set the following:
在此之前,我还设置了以下内容:
- In my
.env
I changed the app URL to:http://127.0.0.1:8000
which is wherephp artisan server
spins up a local server. In
filesystems.php
I changed the public array to the following:'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL').'/storage', 'visibility' => 'public', ],
- 在我
.env
的应用程序 URL 中,我将应用程序 URL 更改为:http://127.0.0.1:8000
这是php artisan server
启动本地服务器的地方。 在
filesystems.php
我将公共数组更改为以下内容:'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL').'/storage', 'visibility' => 'public', ],
I then created the SymLink.
然后我创建了 SymLink。
In my public directory I then had the below:
在我的公共目录中,我有以下内容:
Which is a link to storage/app/public
这是一个链接 storage/app/public
Then I uploaded a file to shares
, so in my storage
folder I now had:
然后我上传了一个文件到shares
,所以在我的storage
文件夹中我现在有:
app/public/shares/name-of-file
app/public/shares/name-of-file
To test this I have the following Controller and View:
为了测试这个,我有以下控制器和视图:
Controller:
控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class TemplateController extends Controller
{
public function index()
{
$files = Storage::allFiles('files/shares');
return view('pages.templates-and-tools.index', compact('files'));
}
}
Relevant part in view
相关部分查看
<table id="templates" class=" table-striped table-vaccancies table-responsive">
<thead>
<tr>
<th class="col-md-1 col-xs-1">Type</th>
<th class="col-md-6 col-xs-6">Name</th>
<th class="col-md-2 col-xs-2">Category</th>
<th class="col-md-2 col-xs-2">Modified</th>
<th class="col-md-1 col-xs-1">Size</th>
</tr>
<tr class="warning no-result">
<td colspan="5">
<i class="fa fa-warning"></i>
<h3 class="text-center"> No result</h3>
</td>
</tr>
</thead>
<tbody>
@foreach ($files as $file)
@php
$date = Storage::lastModified($file); $date = gmdate("d/m/Y - H:i", $date);
$size = Storage::size($file); $size = ceil ($size/1000);
$url = Storage::url($file);
$fileName = pathinfo($file)['filename'];
$Category = pathinfo($file)['dirname'];
$type = pathinfo($file)['extension'];
$Category = basename(dirname( $file))
@endphp
<tr>
<td>{{$type}}</td>
<td>
<a href="{{ Storage::download($url) }} ">{{$fileName }}</a>
</td>
<td>{{$Category}}</td>
<td> {{$date}}</td>
<td>{{$size}}kb</td>
</tr>
@endforeach
</tbody>
</table>
The above grabs everything in the Storage folder under files/shares
, I then just spit out a bunch of information about the file into each table cell.
以上抓取了 Storage 文件夹中的所有内容files/shares
,然后我将有关文件的一堆信息吐出到每个表格单元格中。
The issue I'm facing is with this line:
我面临的问题是这一行:
<a href="{{ Storage::download($url) }}">{{$fileName }}</a>
<a href="{{ Storage::download($url) }}">{{$fileName }}</a>
It returns the following error:
它返回以下错误:
File not found at path: http:/127.0.0.1:8000/storage/files/shares/Hummingbird_Technologies_ES.pdf (View: C:\xampp\htdocs\my-newable\resources\views\pages\templates-and-tools\index.blade.php)
File not found at path: http:/127.0.0.1:8000/storage/files/shares/Hummingbird_Technologies_ES.pdf (View: C:\xampp\htdocs\my-newable\resources\views\pages\templates-and-tools\index.blade.php)
Which is a pretty straightforward error, but...
这是一个非常简单的错误,但是......
You can see that the file exists.
可以看到文件存在。
So, what am I doing wrong?
那么,我做错了什么?
回答by ysfkaya
First of all, check your file is uploaded successfully.
首先,检查您的文件是否上传成功。
And then specific url added in a
tag download
parameter. Like this.
然后在a
标签download
参数中添加特定的 url 。像这样。
<a href="{{ Storage::url($path) }}" download>{{$fileName }}</a>
<a href="{{ Storage::url($path) }}" download>{{$fileName }}</a>
Or added download url in your a
tag. Like this.
或在您的a
标签中添加下载 url 。像这样。
<a href="{{ route('download_url',$path) }}">Download</a>
<a href="{{ route('download_url',$path) }}">Download</a>
Controller.
控制器。
public function download($path)
{
return Storage::download($path);
}