简体   繁体   中英

Force download from amazon s3 private file in laravel

Hello i have some problem with doing a force download. Here is my code :

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);
}

i don't get it how to access the good file

response :

is_file() expects parameter 1 to be a valid path, string given

Any ideas ?

Your headers are not actually attached to the response. If you refer to: https://laravel.com/docs/5.5/responses#attaching-headers-to-responses you will see that the proper way to send headers in Laravel is:

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

The $file is alse sent as a paramenter in reponse() instead of as download().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM