Today, We will explain to you how to upload images using dropzone in laravel 6(Drag and Drop File Upload in Laravel 6 Using Dropzone js). so you can easily upload multiple images using dropzone. Dropzone.js library provides drag and drop facility so it’s a facility using we can easily upload images or files.
We can select, preview, and remove images using the Dropzone.js library. now you can below following the steps.
Overview
Step 1: Install Laravel 6
Step 2: Create Routes
Step 3: Create ImageController
Step 4: Create Blade File
Step 5: Run Our Laravel Application
Step 1 : Install Laravel 6
We are going to install laravel 6, so first open the command prompt or terminal and go to go to xampp htdocs folder directory using the command prompt. after then run the below command.
composer create-project --prefer-dist laravel/laravel laravel6_dropzone_image_upload
Step 2: Create Routes
Add the following route code in the “routes/web.php” file.
<?php use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ // Route::get('/', function () { // return view('welcome'); // }); Route::get('dropzone', 'ImageController@index'); Route::post('dropzone/upload_image', 'ImageController@upload_image')->name('dropzone.upload_image'); Route::get('dropzone/fetch_image', 'ImageController@fetch_image')->name('dropzone.fetch_image'); Route::get('dropzone/delete_image', 'ImageController@delete_image')->name('dropzone.delete_image');
Step 3: Create ImageuploadController
Here in this step, we will create the ImageController.php file. after then we will create an index, upload_image, fetch_image, and delete_image methods in the ImageController.php file. the first method for view file and if image upload then it will use the second method.
so you can follow the below code.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class ImageController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { return view('dropzone'); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function upload_image(Request $request) { $image = $request->file('file'); $imageName = time() . '.' . $image->extension(); $image->move(public_path('images'), $imageName); return response()->json(['success' => $imageName]); } function fetch_image() { $images = \File::allFiles(public_path('images')); $output = '<div class="row">'; foreach($images as $image) { $output .= '<div class="col-md-2"> <img src="'.asset('images/' . $image->getFilename()).'" class="img-thumbnail" width="150" height="150"/> <button type="button" class="btn btn-link remove_image" id="'.$image->getFilename().'">Remove</button> </div>'; } $output .= '</div>'; echo $output; } function delete_image(Request $request) { if($request->get('name')) { \File::delete(public_path('images/' . $request->get('name'))); } } } ?>
Step 4: Create Blade File
Finally, We will create a dropzone.blade.php file in the “resources/views/” folder directory and paste the below code.
<html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Drag And Drop File Upload In Laravel 7 Using Dropzone Js</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.1/dropzone.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.1/dropzone.js"></script> </head> <body> <div class="container-fluid"> <br /> <h3 align="center">Drag And Drop File Upload In Laravel 7 Using Dropzone Js</h3> <br /> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title">Select Image</h3> </div> <div class="panel-body"> <form id="dropzoneForm" class="dropzone" action="{{ route('dropzone.upload_image') }}"> @csrf </form> <div align="center"> <button type="button" class="btn btn-info" id="submit-all">Upload</button> </div> </div> </div> <br /> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title">Uploaded Image</h3> </div> <div class="panel-body" id="uploaded_image"> </div> </div> </div> </body> </html> <script type="text/javascript"> Dropzone.options.dropzoneForm = { autoProcessQueue : false, acceptedFiles : ".png,.jpg,.gif,.bmp,.jpeg", init:function(){ var submitButton = document.querySelector("#submit-all"); myDropzone = this; submitButton.addEventListener('click', function(){ myDropzone.processQueue(); }); this.on("complete", function(){ if(this.getQueuedFiles().length == 0 && this.getUploadingFiles().length == 0) { var _this = this; _this.removeAllFiles(); } load_images(); }); } }; load_images(); function load_images() { $.ajax({ url:"{{ route('dropzone.fetch_image') }}", success:function(data) { $('#uploaded_image').html(data); } }) } $(document).on('click', '.remove_image', function(){ var name = $(this).attr('id'); $.ajax({ url:"{{ route('dropzone.delete_image') }}", data:{name : name}, success:function(data){ load_images(); } }) }); </script>
Step 5: Run Our Laravel Application
We can start the server and run this example using the below command.
php artisan serve
Now we will run our example using the below Url in the browser.
http://127.0.0.1:8000/dropzone