2017-09-27 114 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(),而不是作为下载()。