ItSolutionStuff.com

Laravel Response Download File Example

By Hardik Savani • April 16, 2024
Laravel

We sometimes require to return response with download file from controller method like generate invoice and give to download or etc. Laravel provide us response() with download method that way we can do it.

you can also response download file from storage and delete it after download in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.

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.

In bellow example will help you how it is works.

So, first i am going to create new route for our example as like bellow:

routes/web.php

Route::get('donwload-file', 'DownloadController@downloadFile');

Ok, now i have to add one method "downloadFile()" in my DownloadController. If you don't have DownloadController then you can use your own controller as like bellow:

app/Http/Controllers/DownloadController.php

Example 1: Download File from Storage

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DownloadController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function downloadFile(Request $request)

{

$myFile = storage_path("folder/dummy_pdf.pdf");

return response()->download($myFile);

}

}

Example 2: Download File with name and headers

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DownloadController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function downloadFile(Request $request)

{

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

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

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

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

}

}

Example 2: After Download File will remove

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DownloadController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function downloadFile(Request $request)

{

$myFile = storage_path("folder/dummy_pdf.pdf");

return response()->download($myFile)->deleteFileAfterSend(true);

}

}

So, maybe it can help you....

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Force Redirect HTTP to HTTPS in Laravel?

Read Now →

Laravel Firebase Push Notification Tutorial

Read Now →

Laravel Livewire Datatables Example Tutorial

Read Now →

Laravel 8 Multi Auth (Authentication) Tutorial

Read Now →

How to Delete File from Public Folder / Storage Folder in Laravel?

Read Now →

Laravel Move File from One Folder to Another Example

Read Now →

Laravel Copy File from One Folder to Another Example

Read Now →

Laravel Redirect Back with Input and Error Messages Example

Read Now →

Laravel Redirect to URL using redirect() Helper

Read Now →

Laravel Generate PDF from HTML View File and Download Example

Read Now →

Laravel Multiple Files Download with Response Example

Read Now →

How to Return JSON Response in Laravel?

Read Now →

How to Redirect to External URL in Laravel?

Read Now →

How to Redirect Route with Querystring in Laravel?

Read Now →

Laravel Create JSON File & Download From Text Example

Read Now →