Laravel Response Download File From Storage Example
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..
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial