Response download with file in Laravel 5.3 example

Response download with file in Laravel 5.3 example

In this post we will give you information about Response download with file in Laravel 5.3 example. Hear we will give you detail about Response download with file in Laravel 5.3 exampleAnd how to use it also give you demo for it if it is necessary.

We sometimes require to return response with download file from controller method like generate invoice and give to download or etc. Laravel 5 provide us response() with download method that way we can do it.

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.

In bellow example will help you how it is works.

So, first i am going to create new route for our example as like bellow:

routes/web.php

Route::get('donwload-file', 'HomeController@downloadFile');

Ok, now i have to add one method “downloadFile()” in my HomeController. If you don’t have HomeController then you can use your own controller as like bellow:

app/Http/Controllers/HomeController.php

Also see:Laravel – generate PDF from html view file and download using dompdf

namespace AppHttpControllers;


use IlluminateHttpRequest;


use AppHttpRequests;


class HomeController extends Controller

{


public function downloadFile()

{

$myFile = public_path("dummy_pdf.pdf");

$headers = ['Content-Type: application/pdf'];

$newName = 'onlinecode-pdf-file-'.time().'.pdf';


return response()->download($myFile, $newName, $headers);

}

}

So, maybe it can help you….

Hope this code and post will helped you for implement Response download with file in Laravel 5.3 example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

6  +  4  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US