Hello Devs,
In this tutorial, we are going to learn how to response download file from storage.
Route
routes/web.php
Route::get('downloadFile', 'DownloadFileController@downloadFile');
Controller
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);
}
}
I hope this example helps you.