2017-06-19 147 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>