Laravel 8 Download File Example

Laravel 8 Download File Example

Hello friends, In this example we will show Laravel 8 download file. We will learn how to download file in laravel 8. I would like to share with you example of response download file in laravel 8.

Sometimes we need to return response with download file from controller method such as generate invoice and give to download.

Read Also :  Multiple Where Condition in Laravel Example

Here, we will show how to download file using laravel 8, how to laravel 8 download file, download file response in laravel 8.

Now i am going to create new route for our example such as bellow:

routes/web.php

use App\Http\Controllers\DownloadFileController;
Route::get('/file-download', [DownloadFileController::class, 'index'])->name('file.download.index');

App\Http\Controllers\DownloadFileController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DownloadFileController extends Controller
{
    public function index()
    {
    	$filePath = public_path("dummy.pdf");
    	$headers = ['Content-Type: application/pdf'];
    	$fileName = time().'.pdf';

    	return response()->download($filePath, $fileName, $headers);
    }
}

I hope you understand of laravel 8 download file and it can help you..

Read Also :  Laravel 8 Pagination Example