Laravel 10 Dropzone Image Upload Example Tutorial
Today, We will explain to you how to upload images using dropzone in laravel 10(Laravel 10 Dropzone Image Upload Example). 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 10
Step 2: Create Routes
Step 3: Create ImageController
Step 4: Create Blade File
Step 5: Run Our Laravel Application
Step 1 : Install Laravel 10
We are going to install laravel 10, 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.
1 | composer create-project --prefer-dist laravel/laravel laravel10_dropzone_image_upload |
Step 2: Create Routes
Add the following route code in the “routes/web.php” file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?php use App\Http\Controllers\ImageController; 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::class, 'index']); Route::post('dropzone/upload_image',[ImageController::class, 'upload_image'])->name('dropzone.upload_image'); Route::get('dropzone/fetch_image',[ImageController::class, 'fetch_image'])->name('dropzone.fetch_image'); Route::get('dropzone/delete_image',[ImageController::class, '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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | <?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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <title> 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"> Laravel 10 Dropzone Image Upload Example Tutorial </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.
1 | php artisan serve |
Now we will run our example using the below Url in the browser.
1 | http://127.0.0.1:8000/dropzone |