Skip to content
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Drag And Drop File Upload In Laravel 7 Using Dropzone Js

Drag And Drop File Upload In Laravel 7 Using Dropzone Js

Posted on March 27, 2020September 26, 2021 By XpertPhp 1 Comment on Drag And Drop File Upload In Laravel 7 Using Dropzone Js

Today, We will let you know how to upload images using dropzone in laravel 7. So you can easily upload multiple images using our article. 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 7

Step 2: Create Routes

Step 3: Create ImageController

Step 4: Create Blade File

Step 5: Run Our Laravel Application

Step 1 : Install Laravel 7

We are going to install laravel 7, 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 laravel7_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 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', '[email protected]');
 
Route::post('dropzone/upload_image', '[email protected]_image')->name('dropzone.upload_image');
 
Route::get('dropzone/fetch_image', '[email protected]_image')->name('dropzone.fetch_image');
 
Route::get('dropzone/delete_image', '[email protected]_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>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.

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

Download

Please follow and like us:
error
fb-share-icon
Tweet
fb-share-icon

Recommended Posts:

  • Laravel 6 Datatables Custom filter example tutorial
  • Laravel 9 Toastr Notifications Example Tutorial
  • Laravel 7 MongoDB CRUD Tutorial Example
  • Laravel one to one eloquent relationship tutorial example
  • How To Send Email Using Mailtrap In Laravel 9
Ajax, Laravel Tags:Drag and Drop File Upload, Dropzone File Upload, dropzone js, Image Upload, laravel 7, Laravel 7 Image Upload, laravel ajax upload, laravel drag drop upload, laravel file upload, multiple image upload, preview image

Post navigation

Previous Post: Laravel 7 Where Clauses Methods Example Tutorial
Next Post: How To Resize An Image To Thumbnail In Laravel 7

Latest Posts

  • How to Convert Date and Time from one timezone to another in php
  • how to get current date and time in php
  • Drag and Drop Reorder Items with jQuery, PHP & MySQL
  • Laravel 9 Toastr Notifications Example Tutorial
  • Laravel 9 CRUD Operation Example Using Google Firebase
  • Laravel 9 CKeditor Image Upload With Example
  • Laravel 9 Summernote Image Upload With Example
  • Laravel 9 Stripe Payment Gateway Integrate Example
  • How To Send Email Using Mailtrap In Laravel 9
  • Laravel 9 Fullcalendar Ajax Example Tutorial

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Age Calculator Online
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tricks jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch Laravel Socialite laravel social login nodejs pagination payment gateway php with mysql react js tutorial rewrite rule send mail validation wysiwyg editor

Copyright © 2018 - 2022,

All Rights Reserved Powered by XpertPhp.com