Drag And Drop File Upload In Laravel 7 Using Dropzone Js – onlinecode

Drag And Drop File Upload In Laravel 7 Using Dropzone Js – onlinecode

In this post we will give you information about Drag And Drop File Upload In Laravel 7 Using Dropzone Js – onlinecode. Hear we will give you detail about Drag And Drop File Upload In Laravel 7 Using Dropzone Js – onlinecodeAnd how to use it also give you demo for it if it is necessary.

Today, We will explain to you how to upload images using dropzone in laravel 7. 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 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.

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.

<?php

use IlluminateSupportFacadesRoute;

/*
|--------------------------------------------------------------------------
| 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 AppHttpControllers;
  
use IlluminateHttpRequest;
  
class ImageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return IlluminateHttpResponse
     */
    public function index()
    {
       return view('dropzone');
    }
  
    /**
     * Display a listing of the resource.
     *
     * @return IlluminateHttpResponse
     */
    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 >';
     foreach($images as $image)
     {
      $output .= '<div >
                <img src="'.asset('images/' . $image->getFilename()).'"  width="150" height="150"/>
                <button type="button"  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 >
      <br />
    <h3 align="center">Drag And Drop File Upload In Laravel 7 Using Dropzone Js</h3>
    <br />
        
      <div >
        <div >
          <h3 >Select Image</h3>
        </div>
        <div >
          <form id="dropzoneForm"  action="{{ route('dropzone.upload_image') }}">
            @csrf
          </form>
          <div align="center">
            <button type="button"  id="submit-all">Upload</button>
          </div>
        </div>
      </div>
      <br />
      <div >
        <div >
          <h3 >Uploaded Image</h3>
        </div>
        <div  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 ApplicationWe 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

Download

Read AlsoLaravel 7 CRUD Operation With Ajax Example

Laravel 7 Pagination Example Tutorial

Please follow and like us:

Hope this code and post will helped you for implement Drag And Drop File Upload In Laravel 7 Using Dropzone Js – onlinecode. 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 *

4  +    =  13

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