我在存储文件夹中下载文件时遇到问题。我没有收到任何错误消息。所有这一切都是重定向到404页面。该文件存在于给定的路径中,所以我很难过。我已经尝试从response()中的第二个参数中删除$ document但是没有工作。我需要做什么?奇怪的是,在本地主机上运行时它有效吗?
public function download(Request $request, $document)
{
$pathToFile = storage_path() . '/' . 'app/documents/client'.'/'.$document;
if (file_exists($pathToFile))
{
return response()->download($pathToFile, $document);
}
else
{
// Error
return redirect('errors.404');
}
}
文件系统
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'visibility' => 'public',
],
路线:
Route::get('documents/client/{document}', 'ClientDocumentController@download')->name('client.docs');
答案 0 :(得分:-1)
这是一种下载文件的方法,它可以提供帮助。
最初的问题是锚标记中的href正在创建指向url的链接,但是文档无法通过普通网址访问,因为它位于存储中。然后代码重定向到404,因为它找不到url资源(存储不公开)。控制器从未收到请求,因为href不向控制器发送请求。
因此,使用表单操作,将请求下载到控制器,控制器又从存储处理GET。
<form action="{{ route('client.docs', [$document->document] ) }}" method="GET" role="form" id="form" >
{{ csrf_field() }}
<input type="hidden" placeholder="{{ $document->document}}" >
<button class="btn btn-xs btn-primary" type="submit">
Download<i class="fa fa-arrow-circle-down"></i>
</button>
</form>