DropzoneJS is an open source library that provides drag’n’drop file uploads with image previews. It’s lightweight, doesn’t depend on any other library (like jQuery) and is highly customizable. For full documentation please check the
plugin's official site.
Usage
DropzoneJS's CSS and Javascript files are bundled in the global plugin bundles and globally included in all pages:
DropzoneJS CSS is highly customized in
sass/vendors/plugins/_dropzone.scssSCSS file in order to use it as native component within the design system. The SCSS code is compiled into
assets/plugins/global/plugins.bundle.cssand globally included in all pages.
DropzoneJS Javascript is globally initialized with some predefined settings in
src/js/vendors/plugins/dropzone.init.jsand the initialization code is bundled within
assets/plugins/global/plugins.bundle.jsand globally included in all pages.
Basic Example
DropzoneJS example with manual file attachment and upload controls.
var myDropzone =newDropzone("#kt_dropzonejs_example_1",{url:"https://keenthemes.com/scripts/void.php",// Set the url for your upload script locationparamName:"file",// The name that will be used to transfer the filemaxFiles:10,maxFilesize:10,// MBaddRemoveLinks:true,accept:function(file, done){if(file.name =="wow.jpg"){done("Naha, you don't.");}else{done();}}});
<!--begin::Form--><formclass="form"action="#"method="post"><!--begin::Input group--><divclass="fv-row"><!--begin::Dropzone--><divclass="dropzone"id="kt_dropzonejs_example_1"><!--begin::Message--><divclass="dz-message needsclick"><!--begin::Icon--><iclass="bi bi-file-earmark-arrow-up text-primary fs-3x"></i><!--end::Icon--><!--begin::Info--><divclass="ms-4"><h3class="fs-5 fw-bolder text-gray-900 mb-1">Drop files here or click to upload.</h3><spanclass="fs-7 fw-bold text-gray-400">Upload up to 10 files</span></div><!--end::Info--></div></div><!--end::Dropzone--></div><!--end::Input group--></form><!--end::Form-->
Queue Upload
DropzoneJS example with multiple file attachment with preview and cancel action.
// set the dropzone container idconst id ="#kt_dropzonejs_example_2";const dropzone = document.querySelector(id);// set the preview element templatevar previewNode = dropzone.querySelector(".dropzone-item");
previewNode.id ="";var previewTemplate = previewNode.parentNode.innerHTML;
previewNode.parentNode.removeChild(previewNode);var myDropzone =newDropzone(id,{// Make the whole body a dropzoneurl:"https://keenthemes.com/scripts/void.php",// Set the url for your upload script locationparallelUploads:20,previewTemplate: previewTemplate,maxFilesize:1,// Max filesize in MBautoQueue:false,// Make sure the files aren't queued until manually addedpreviewsContainer: id +" .dropzone-items",// Define the container to display the previewsclickable: id +" .dropzone-select"// Define the element that should be used as click trigger to select files.});
myDropzone.on("addedfile",function(file){// Hookup the start button
file.previewElement.querySelector(id +" .dropzone-start").onclick=function(){ myDropzone.enqueueFile(file);};const dropzoneItems = dropzone.querySelectorAll('.dropzone-item');
dropzoneItems.forEach(dropzoneItem=>{
dropzoneItem.style.display ='';});
dropzone.querySelector('.dropzone-upload').style.display ="inline-block";
dropzone.querySelector('.dropzone-remove-all').style.display ="inline-block";});// Update the total progress bar
myDropzone.on("totaluploadprogress",function(progress){const progressBars = dropzone.querySelectorAll('.progress-bar');
progressBars.forEach(progressBar=>{
progressBar.style.width = progress +"%";});});
myDropzone.on("sending",function(file){// Show the total progress bar when upload startsconst progressBars = dropzone.querySelectorAll('.progress-bar');
progressBars.forEach(progressBar=>{
progressBar.style.opacity ="1";});// And disable the start button
file.previewElement.querySelector(id +" .dropzone-start").setAttribute("disabled","disabled");});// Hide the total progress bar when nothing's uploading anymore
myDropzone.on("complete",function(progress){const progressBars = dropzone.querySelectorAll('.dz-complete');setTimeout(function(){
progressBars.forEach(progressBar=>{
progressBar.querySelector('.progress-bar').style.opacity ="0";
progressBar.querySelector('.progress').style.opacity ="0";
progressBar.querySelector('.dropzone-start').style.opacity ="0";});},300);});// Setup the buttons for all transfers
dropzone.querySelector(".dropzone-upload").addEventListener('click',function(){
myDropzone.enqueueFiles(myDropzone.getFilesWithStatus(Dropzone.ADDED));});// Setup the button for remove all files
dropzone.querySelector(".dropzone-remove-all").addEventListener('click',function(){
dropzone.querySelector('.dropzone-upload').style.display ="none";
dropzone.querySelector('.dropzone-remove-all').style.display ="none";
myDropzone.removeAllFiles(true);});// On all files completed upload
myDropzone.on("queuecomplete",function(progress){const uploadIcons = dropzone.querySelectorAll('.dropzone-upload');
uploadIcons.forEach(uploadIcon=>{
uploadIcon.style.display ="none";});});// On all files removed
myDropzone.on("removedfile",function(file){if(myDropzone.files.length <1){
dropzone.querySelector('.dropzone-upload').style.display ="none";
dropzone.querySelector('.dropzone-remove-all').style.display ="none";}});
<!--begin::Form--><formclass="form"action="#"method="post"><!--begin::Input group--><divclass="form-group row"><!--begin::Label--><labelclass="col-lg-2 col-form-label text-lg-right">Upload Files:</label><!--end::Label--><!--begin::Col--><divclass="col-lg-10"><!--begin::Dropzone--><divclass="dropzone dropzone-queue mb-2"id="kt_dropzonejs_example_2"><!--begin::Controls--><divclass="dropzone-panel mb-lg-0 mb-2"><aclass="dropzone-select btn btn-sm btn-primary me-2">Attach files</a><aclass="dropzone-upload btn btn-sm btn-light-primary me-2">Upload All</a><aclass="dropzone-remove-all btn btn-sm btn-light-primary">Remove All</a></div><!--end::Controls--><!--begin::Items--><divclass="dropzone-items wm-200px"><divclass="dropzone-item"style="display:none"><!--begin::File--><divclass="dropzone-file"><divclass="dropzone-filename"title="some_image_file_name.jpg"><spandata-dz-name>some_image_file_name.jpg</span><strong>(<spandata-dz-size>340kb</span>)</strong></div><divclass="dropzone-error"data-dz-errormessage></div></div><!--end::File--><!--begin::Progress--><divclass="dropzone-progress"><divclass="progress"><divclass="progress-bar bg-primary"role="progressbar"aria-valuemin="0"aria-valuemax="100"aria-valuenow="0"data-dz-uploadprogress></div></div></div><!--end::Progress--><!--begin::Toolbar--><divclass="dropzone-toolbar"><spanclass="dropzone-start"><iclass="bi bi-play-fill fs-3"></i></span><spanclass="dropzone-cancel"data-dz-removestyle="display: none;"><iclass="bi bi-x fs-3"></i></span><spanclass="dropzone-delete"data-dz-remove><iclass="bi bi-x fs-1"></i></span></div><!--end::Toolbar--></div></div><!--end::Items--></div><!--end::Dropzone--><!--begin::Hint--><spanclass="form-text text-muted">Max file size is 1MB and max number of files is 5.</span><!--end::Hint--></div><!--end::Col--></div><!--end::Input group--></form><!--end::Form-->
Queue Auto Upload
DropzoneJS example with manual file attachment and auto upload.
// set the dropzone container idconst id ="#kt_dropzonejs_example_3";const dropzone = document.querySelector(id);// set the preview element templatevar previewNode = dropzone.querySelector(".dropzone-item");
previewNode.id ="";var previewTemplate = previewNode.parentNode.innerHTML;
previewNode.parentNode.removeChild(previewNode);var myDropzone =newDropzone(id,{// Make the whole body a dropzoneurl:"https://keenthemes.com/scripts/void.php",// Set the url for your upload script locationparallelUploads:20,maxFilesize:1,// Max filesize in MBpreviewTemplate: previewTemplate,previewsContainer: id +" .dropzone-items",// Define the container to display the previewsclickable: id +" .dropzone-select"// Define the element that should be used as click trigger to select files.});
myDropzone.on("addedfile",function(file){// Hookup the start buttonconst dropzoneItems = dropzone.querySelectorAll('.dropzone-item');
dropzoneItems.forEach(dropzoneItem=>{
dropzoneItem.style.display ='';});});// Update the total progress bar
myDropzone.on("totaluploadprogress",function(progress){const progressBars = dropzone.querySelectorAll('.progress-bar');
progressBars.forEach(progressBar=>{
progressBar.style.width = progress +"%";});});
myDropzone.on("sending",function(file){// Show the total progress bar when upload startsconst progressBars = dropzone.querySelectorAll('.progress-bar');
progressBars.forEach(progressBar=>{
progressBar.style.opacity ="1";});});// Hide the total progress bar when nothing"s uploading anymore
myDropzone.on("complete",function(progress){const progressBars = dropzone.querySelectorAll('.dz-complete');setTimeout(function(){
progressBars.forEach(progressBar=>{
progressBar.querySelector('.progress-bar').style.opacity ="0";
progressBar.querySelector('.progress').style.opacity ="0";});},300);});
<!--begin::Form--><formclass="form"action="#"method="post"><!--begin::Input group--><divclass="form-group row"><!--begin::Label--><labelclass="col-lg-2 col-form-label text-lg-right">Upload Files:</label><!--end::Label--><!--begin::Col--><divclass="col-lg-10"><!--begin::Dropzone--><divclass="dropzone dropzone-queue mb-2"id="kt_dropzonejs_example_3"><!--begin::Controls--><divclass="dropzone-panel mb-lg-0 mb-2"><aclass="dropzone-select btn btn-sm btn-primary me-2">Attach files</a><aclass="dropzone-remove-all btn btn-sm btn-light-primary">Remove All</a></div><!--end::Controls--><!--begin::Items--><divclass="dropzone-items wm-200px"><divclass="dropzone-item"style="display:none"><!--begin::File--><divclass="dropzone-file"><divclass="dropzone-filename"title="some_image_file_name.jpg"><spandata-dz-name>some_image_file_name.jpg</span><strong>(<spandata-dz-size>340kb</span>)</strong></div><divclass="dropzone-error"data-dz-errormessage></div></div><!--end::File--><!--begin::Progress--><divclass="dropzone-progress"><divclass="progress"><divclass="progress-bar bg-primary"role="progressbar"aria-valuemin="0"aria-valuemax="100"aria-valuenow="0"data-dz-uploadprogress></div></div></div><!--end::Progress--><!--begin::Toolbar--><divclass="dropzone-toolbar"><spanclass="dropzone-delete"data-dz-remove><iclass="bi bi-x fs-1"></i></span></div><!--end::Toolbar--></div></div><!--end::Items--></div><!--end::Dropzone--><!--begin::Hint--><spanclass="form-text text-muted">Max file size is 1MB and max number of files is 5.</span><!--end::Hint--></div><!--end::Col--></div><!--end::Input group--></form><!--end::Form-->