Laravel 5 force download file with the response helper method

Laravel 5 force download file with the response helper method

In this post we will give you information about Laravel 5 force download file with the response helper method. Hear we will give you detail about Laravel 5 force download file with the response helper methodAnd how to use it also give you demo for it if it is necessary.

In this Laravel tutorial, I will tell you about the download() method that is used to generate a response to force download the file in Laravel application.

Sometime, You need to download the report or invoices from the controller then you can use this download() method to download files directly.

The download method accept the three arguments.

In first argument, You will pass the path of download file and the second argument will hold the file name, Finally the third argument hold the HTTP headers. You can change the filename of download file by passing it as a second argument in download().


Routes

For this example, define a simple route to download file from controller.


routes/web.php

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


Home Controller

Now add following code in your HomeController.php


app/Http/Controllers/HomeController.php

  1. <?php
  2. namespace AppHttpControllers;
  3. use IlluminateHttpRequest;
  4. class HomeController extends Controller
  5. {
  6. public functionforceDownloadFile()
  7. {
  8.     $filePath=public_path("my_invoice.pdf");
  9.     $headers=['Content-Type: application/pdf'];
  10.     $fileName=time().'.pdf';
  11.     returnresponse()->download($filePath,$fileName,$headers);
  12. }
  13. }
<?php
namespace AppHttpControllers;

use IlluminateHttpRequest;

class HomeController extends Controller
{

    public function forceDownloadFile()
    {
    	$filePath = public_path("my_invoice.pdf");
    	$headers = ['Content-Type: application/pdf'];
    	$fileName = time().'.pdf';

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

How to create a JSON text file for download in Laravel 5.3?

Hope this code and post will helped you for implement Laravel 5 force download file with the response helper method. 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 *

  +  56  =  60

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