Laravel Response Download File From Storage Example

Laravel 6 , Laravel , Laravel 7

Nicesnippets

74

21-05-2020


Hi Guys

In this blog, I will show how to response download file from storage example. We sometimes require to return response with download file from controller method like generate invoice and give to download or etc.

We will explain response download fill in the storage.In First argument of download() we have to give path of download file. We can rename of download file by passing second argument of download(). We can also set headers of file by passing third argument.I have to add one method "downloadFile()" in my downloadfilecontroller.

In here example to response download file from storage.

Route

So, first I am create route for our example.

routes/web.php

Route::get('downloadFile', 'DownloadFileController@downloadFile');

Controller

Now In this controller downloadFile() metod to response download file from storage.

app/Http/Controllers/DownloadFileController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DownloadFileController extends Controller

{

public function downloadFile()

{

$myFile = public_path("dummy.pdf");

$headers = ['Content-Type: application/pdf'];

$newName = 'nicesnippets-pdf-file-'.time().'.pdf';

return response()->download($myFile, $newName, $headers);

}

}

it will help you..


Recommended Posts