2017-06-19 150 views
0

我在下载存储文件夹中的文件时遇到问题。我没有收到错误消息。发生的只是重定向到404页面。该文件存在于给定的路径中,所以我很难过。我试图从响应()的第二个参数中删除$文档,但没有奏效。我需要做什么?奇怪的是,在本地主机上运行它的作品?Laravel下载文件问题

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}',  '[email protected]')->name('client.docs'); 
+0

记录你的'$ pathToFile',看它是否真的存在,以及权限是什么。 – aynber

+0

检查文件是否存在于正确的位置? –

+0

该文件存在。但是这个路线是否正确?更新问题 –

回答

0

下面是下载文件的人可能会有所帮助的方法。

最初的问题是锚标记中的href创建了一个链接到url,但该文档不能通过正常的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>