2017-09-27 112 views
0

你好,我有一些問題,做強制下載。 這裏是我的代碼:強制從亞馬遜s3私人文件下載laravel

public function download(Request $request, $id) { 
    $document = Document::find($id); 
    $s3 = Storage::disk('s3'); 
    $name = substr($document->link, strrpos($document->link, '/') + 1); 
    $file = $s3->get($name); 
    $headers = [ 
    'Content-Type' => 'application/pdf', 
    'Content-Description' => 'File Transfer', 
    'Content-Disposition' => "attachment; filename={$name}", 
    'filename'=> $name 
    ]; 
    return response()->download($file); 
} 

我不明白如何訪問好的文件

響應:

is_file()預計參數1是一個有效的路徑,串給定

任何想法?

回答

1

您的標題實際上未附加到響應中。如果您參考:https://laravel.com/docs/5.5/responses#attaching-headers-to-responses你會看到,在Laravel發送頭的正確方法是:

return response($file) 
      ->header('Content-Type', 'application/pdf') 
      ->header('Content-Description', 'File Transfer') 
      ->header('Content-Disposition', "attachment; filename={$name}") 
      ->header('Filename', $name) 

在$文件中的效應初探被ALSE發來的paramenter(),而不是作爲下載()。