Prasad Patel
Prasad Patel

Reputation: 697

Laravel 5.4 download csv file is not working

I have a requirement of after user clicks on a link to download a csv file then that csv file has to be downloaded located at our application path. I have written code for it but its not working getting routing related error it is not going inside of the specified controller method. my code is

<a href="/alerts/download">Download</a>

My Controller:

public function getDownload(Request $request)
{
    //CSV file is stored under project/public/download/sample.csv
    $file= public_path(). "/download/sample.csv";
   return response()->download($file);
}

My Route

Route::get('alerts/download', 'AlertsController@getDownload');

I am getting the error : enter image description here

enter image description here

I googled and tried some solutions but not working though. Any Help would be appreciated. Thanks.

Upvotes: 0

Views: 1608

Answers (1)

Prasad Patel
Prasad Patel

Reputation: 697

I have binded the controller with model while creating my controller by using command "php artisan make:controller BookController --resource --model=Book" so I have deleted my controller and cleared routes with command "php artisab route:clear" after that I have created the controller once again by using the command "php artisan make:controller MyController --resource". I have used the below code:

<a href="/alerts/download/sample.csv">Download</a>

Routes:

Route::get('/alerts/download/{file}', 'AlertsController@getDownload');

MyController:

public function getDownload($file_name)
{
   //CSV file is stored under project/public/download/sample.csv
   $file_path = public_path('download/'.$file_name);
   return response()->download($file_path);
}

I found solution here : Download files in laravel using Response::download

Thanks to stackoverflow. Laravel Rockzz.

Upvotes: 2

Related Questions