Download file from cloud disk (S3) with Laravel 5.1

siannone

I'm having some problems generating a file download response in Laravel 5.1 while trying to download a file from Amazon S3.

This is my controller action:

/**
 * @param GetRequest $request
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function get(GetRequest $request)
{
    $fileEntry = $this->fileRepository->find();
    $file = Storage::disk('s3')->get('projects/'.$fileEntry->project.'/'.$fileEntry->name);

    return $this->respondDownload($file, $fileEntry->name, $fileEntry->type);
}

And this is the respondDownload method:

/**
 * Respond with a file download.
 *
 * @param $fileContent
 * @param $fileName
 * @param $mime
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function respondDownload($fileContent, $fileName, $mime)
{
    return (new Response($fileContent, 200))
        ->header('Content-Type', $mime)
        ->header('Content-Disposition', 'attachment; filename="'.$fileName.'"');
}

If I open the developer tools I can see that the response is successful and the content is set to the file content, but the browser is not prompting to save the file somewhere.

How can I solve this problem? Is something wrong with the headers?

Edit

I also tried making a streamed response but the browser is still not showing a save file modal.

public function respondDownload($fileContent, $fileName, $mime)
{
    return (new StreamedResponse(function() use ($fileContent)
    {
        echo $fileContent;
    }, 200, [
        'Content-Type' => $mime,
        'Content-Disposition' => 'attachment; filename="'.$fileName.'"'
    ]));
}

Edit 2

Response headers: Response headers

Response content: Response content

siannone

The actual problem was caused by how I sent the download request to the server.

Previously I was making an AJAX request to the server and that wasn't making the browser to prompt me where to save the downloaded file.

In order to fix that I simply had to start the download request from an a tag.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Download S3 file links in Laravel

From Dev

Display pdf file from local disk in Laravel 5?

From Dev

Laravel - S3 multiple file zip and download

From Dev

Boto3 : Download file from S3

From Dev

Download 5M of 1MB-sized archive files from an external FTP server to AWS S3

From Dev

Laravel 5 Multiple Download File

From Dev

Python: how to download a file from an S3 bucket

From Dev

How to download media file from S3 bucket in Django

From Dev

Python get s3 file from download link

From Dev

How to generate URL to download file from S3 bucket

From Dev

Download Inventory File from AWS S3

From Java

How to download GZip file from S3?

From Javascript

Javascript to download a file from amazon s3 bucket?

From Dev

Download file from Amazon S3 through Carrierwave and Fog

From Dev

Amazon s3 file download from android

From Dev

Python - Download file from AWS S3 for the current date

From

Download file from AWS S3 using Python

From Dev

Download file from Amazon S3 using REST API

From Dev

'no such file or directory': Download multiple files from S3

From Dev

Download apk file from S3 using DownloadManager

From Dev

Download private file from S3 using bash

From Dev

Unable to download a file from S3 by the URL in a browser

From Dev

AWS S3 File Download from the client-side

From Dev

File download from API to Meteor server and upload to S3

From Dev

Download a file from S3 to Local machine

From Dev

Download File from FileResult without saving to disk

From Dev

Upload file to amazon s3 server using laravel 5

From Dev

Unable to download file from s3 using return response()->download($file);

From Dev

website page command (and syntax) to download file from AWS s3 to user's download bucket

Related Related

  1. 1

    Download S3 file links in Laravel

  2. 2

    Display pdf file from local disk in Laravel 5?

  3. 3

    Laravel - S3 multiple file zip and download

  4. 4

    Boto3 : Download file from S3

  5. 5

    Download 5M of 1MB-sized archive files from an external FTP server to AWS S3

  6. 6

    Laravel 5 Multiple Download File

  7. 7

    Python: how to download a file from an S3 bucket

  8. 8

    How to download media file from S3 bucket in Django

  9. 9

    Python get s3 file from download link

  10. 10

    How to generate URL to download file from S3 bucket

  11. 11

    Download Inventory File from AWS S3

  12. 12

    How to download GZip file from S3?

  13. 13

    Javascript to download a file from amazon s3 bucket?

  14. 14

    Download file from Amazon S3 through Carrierwave and Fog

  15. 15

    Amazon s3 file download from android

  16. 16

    Python - Download file from AWS S3 for the current date

  17. 17

    Download file from AWS S3 using Python

  18. 18

    Download file from Amazon S3 using REST API

  19. 19

    'no such file or directory': Download multiple files from S3

  20. 20

    Download apk file from S3 using DownloadManager

  21. 21

    Download private file from S3 using bash

  22. 22

    Unable to download a file from S3 by the URL in a browser

  23. 23

    AWS S3 File Download from the client-side

  24. 24

    File download from API to Meteor server and upload to S3

  25. 25

    Download a file from S3 to Local machine

  26. 26

    Download File from FileResult without saving to disk

  27. 27

    Upload file to amazon s3 server using laravel 5

  28. 28

    Unable to download file from s3 using return response()->download($file);

  29. 29

    website page command (and syntax) to download file from AWS s3 to user's download bucket

HotTag

Archive