In this tutorial, we will see how to download file in laravel 8. We will see example of response download with file in laravel 8.
Firstly, create a new route as given in example below:
routes/web.php
use App\Http\Controllers\DownloadFileController;
Route::get('/file-download', [DownloadFileController::class, 'index'])->name('file.download.index');
Now add "downloadFile()" method in DownloadFileController.
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 this example helps you.