How to drag and drop multiple files using Dropzone.js

While uploading files instead of selecting files from file manager, we wish to drag those files and drop them in web page, This blog post will give you an idea of how to make a div or particular form to accept the files that are dragged on to them from File Manager. we will use Dropzone.js library which is a lightweight open source library that turns a DOM element in a web page into Dropzone. This enables the user to drag the files on to that area and those files will be uploaded to server via Ajax.

Download the dropzone File here Dropzone.js and include the path to dropzone.js file.

Now dropzone is activated and ready to be used in your web page. Dropzone does not support server side validations and data handling it only enables you to handle your dragged files in client side. you need to implement the code to recieve or handle the data in server side.

How to Use Dropzone:

Just giving class name as dropzone to the Form will do it all. Dropzone will find all the form elements in form which containing class dropzone and attach itself to that. and dropzone will upload the files dragged into that to the given action attribute.

we can even create dropzones on non form DOM elements by instantiating dropzone class.

var myDropzone = new Dropzone("div#yourId", { url: "/file/upload/"})

or we can instantiate dropzone class with jquery like :

$("div#yourId").dropzone({ url: "/file/upload/" })

you must provide action url option while using dropzone on non form elements as dropzone do not know where to post the dragged files without action attribute.

Dropzone Configuration:

we can instantiate dropzone on a DOM element in two ways as mentioned earlier one is by giving lass dropzone to the desired element or instantiating the dropzone object on that element, we can provide configure options here while creating dropzone on that element. If we have the elements with dropzone class we need to store configurations manually so the dropzone wil know how to configure dropzone.

Dropzone.options.myDropzone = { paramName: "file", maxFilesize: 2, // MB }

Dropzone automatically discovers the elements with dropzone class and automatically attach itself to it. if ou want to stop this and wish to manually instantiate dropzone you can achieve that by using disabling autodiscover behaviour of the dropzone. you can do that on particular element or on the whole page.

Dropzone.options.myDropzone = false; //preventing dropzone from auto discovering this element
Dropzone.autoDiscover = false //preventing dropzone from auto discover on all elements

There are a lot of Configuration options for dropzone, here i am only listing few most useful options, you can check full configurations and Documentation of Dropzone Here.

  1. Url
    • We must provide URL option on non-form elements and when the form does not have an action attribute
  2. parallelUploads
    • number of files uploaded to the server at a time, files are sent to the server in the queue. files will be processed one after other or 'n' number of files if given more than one here.
  3. uploadMultiple
    • This enables to send multiple files in a single request.
  4. maxFilesize
    • This number defines the maximum file size you are allowed to upload, if the file size exceeds this value it returns FileSizeExceeds error message
  5. paramName
    • The name of the file attribute that gets uploaded to server, default name is 'file', you can change that name by giving this option.
  6. createImageThumbnails
    • Thumbnails will be created for uploaded images if set to True.
  7. maxFiles
    • Defines the Max Number of files you are allowed to upload
  8. acceptedFiles
    • We can mention the files mime types we wanted to accept, other files selected will get an error message. This acts as accept parameter in HTML file tag.
  9. autoProcessQueue
    • The default is True, FIles automatically processed to the server by ajax, If this set to false processing will not be invoked automatically, you have to invoke the processing by calling myDropzone.processQueue()
  10. addRemoveLinks
    • If Given True, processed files will get a link to remove that file preview from dropzone.

Posted On 30 June 2018 By MicroPyramid

About Micropyramid

Micropyramid is a software development and cloud consulting partner for enterprise businesses across the world. We work on python, Django, Salesforce, Angular, Reactjs, React Native, MySQL, PostgreSQL, Docker, Linux, Ansible, git, amazon web services. We are Amazon and salesforce consulting partner with 5 years of cloud architect experience. We develop e-commerce, retail, banking, machine learning, CMS, CRM web and mobile applications.

Need any Help in your Project?Let's Talk


Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
*We don't provide your email contact details to any third parties