Laravel

Laravel 5.6 Image Download Tutorial

January 31, 2018

author:

Laravel 5.6 Image Download Tutorial

We will now implement downloading an image from a Laravel application. We will continue working on our Book App and add all the required stuff to that. As we concluded in the previous segments with creating CRUD and uploading an image with Laravel, in this segment, we will complete the life-cycle.

# Download Button

To get started, let us first add a download button to our book detail page.

# resources/views/books/show.blade.php
...
<a href="{{ route('books.download', $book->id) }}" class="btn btn-outline-warning">Download Cover</a>

# Download Route

As we added a href link for download, let us now create a corresponding route in web.php

# routes/web.php
...
Route::get('books/save/{id}',[
    'as' => 'books.download', 'uses' => 'BookController@downloadImage']);
Route::resource('books','BookController');

It is important to note that in the previous segments, we have used Resource Controllers and Routes. It automatically generates required web routes and controller methods. And since we want to create an extra route for the same controller, we have to add this web route BEFORE the resource route. Only then it will be accessible.

# Download Method in Controller

Let us now implement the actual downloading feature. You’ll be surprised to see how simple it is to add download functionality in Laravel. All we have to do is call a download() method while returning a response from the controller .

# app/Http/Controllers/BookController.php
...
public function downloadImage($imageId){
   $book_cover = Book::where('id', $imageId)->firstOrFail();
   $path = public_path(). '/uploads/'. $book_cover->filename;
   return response()->download($path, $book_cover
            ->original_filename, ['Content-Type' => $book_cover->mime]);
}

download() method accepts path of the file to be sent to the browser for downloading. It also takes optional arguments like filename and HTTP headers. Note that filename in this argument is the name that is seen by the user while downloading the file.

# Final Testing

It’s time to test the application in the browser. Run an application in the browser and insert book details with an image if you don’t have it already. Next, view any specific book and click on download button.
Laravel Download File
Your browser will automatically download the image when you click on the download button:

Laravel Download File

Conclusion:
We just completed a series of working with some major components of Laravel. We covered Database operations (CRUD), working with web routes and controllers. We also created blade template and used it in various views with bootstrap. At last, we also stopped upon upload and downloaded feature using Laravel Framework.

Questions & Comments:

Thank you for reading. If you suggestions and questions regarding file downloads in Laravel, share them in the comment section below.

Leave a comment

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