[英]Download file from a link in laravel 5.3
我想创建一个超链接以下载我的存储/应用/公共文件夹中的文件。 我在刀片中使用它
<a href="{{ asset('storage/app/public/1489082996.docx') }}" download="{{ asset('storage/app/public/1489082996.docx') }}"> Read Paper</a>
但这告诉我找不到错误文件。我该怎么办是我的filesystem.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'visibility' => 'public',
],
一种简单的方法,使您可以将文件存储在存储文件夹中,并且无需进行系统更改即可下载:(在laravel 5.3中)
您可以建立指向文件的普通链接:
<a href="/download/1489082996.docx"> Read Paper</a>
然后,您需要配置您的route / web.php
Route::get('/download/{file}', function ($file='') {
return response()->download(storage_path('app/public/'.$file));
});
这将打开所有类型文件的下载对话框,包括img和pdf的文件。 如果要在线显示文件而不下载,则应使用file方法:
return response()->file(storage_path('app/public/'.$file));
资产路径是相对于/public
目录的。 因此,假设您已正确地将存储目录与php artisan storage:link
asset('1489082996.docx')
链接, asset('1489082996.docx')
需要asset('1489082996.docx')
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.