简体   繁体   中英

Not able to add event handler in dropzone

I am making this addDropzone() function called on a button click which passes a value as mutilple dropzone elements are needed, but only one at time but on different div's so 'val' parameter in function helps define div id's. I am able to run the dropzone element. It also saves the files on the server, but i am not able to get event handler's working.

This is the function that I am using.

Also if anybody could suggest me a way to read server message in event handler

JAVASCRIPT CODE

    function addDropzone(val){
    document.getElementById("div2").innerHTML = '<div class="item" style="width:96%; margin:5px auto; position:relative; height: auto; background-color:lightgray;"><form action="file-upload.php" class="dropzone" id="my-dropzone-'+val+'"></form></div>';        
    var myDropzone = new Dropzone("div #my-dropzone-"+val);     
    Dropzone.options.myDropzone = {
            paramName: 'file',
            method: 'post',
            maxFiles: 1,
            url: 'file-upload.php',
            dictDefaultMessage: "Drag your images",
            clickable: false,
            maxFilesize: 512,
            uploadMultiple: false,
            addRemoveLinks: true,
            forceFallback:true      
    };
    Dropzone.options.myDropzone = false;
    Dropzone.options.myDropzone.enable();

    Dropzone.options.myDropzone = {
        init: function(){
        this.on("maxfilesexceeded", function(file) { alert("File limit 1"); });
        this.on("complete", function(file) {alert(serverReponse);});
        }
    };

}

Didn't know about dropzone, but as I understand the documentation, I would have written it like this :

function addDropzone(val){
  var dropZoneId= "my-dropzone-"+val;
  // this line has moved-up, and the option is not referenced in the same way
  Dropzone.options[dropZoneId] = false;
  // proper way of adding new content to div2 without losing event handlers
  var div = document.createElement('div');
  div.innerHTML = '<div class="item" style="width:96%; margin:5px auto; position:relative; height: auto; background-color:lightgray;"><form action="file-upload.php" class="dropzone" id="'+dropZoneId+'"></form></div>';
  document.getElementById("div2").appendChild(div.firstChild);           
  var myDropzone = new Dropzone("#"+dropZoneId,{
        paramName: 'file',
        method: 'post',
        maxFiles: 1,
        url: 'file-upload.php',
        dictDefaultMessage: "Drag your images for zone "+val,
        clickable: false,
        maxFilesize: 512,
        uploadMultiple: false,
        addRemoveLinks: true,
        forceFallback:false,
        init: function(){
           // from the doc, quick debug
           this.on("addedfile", function(file) { alert("added file in zone."+val); });
           this.on("maxfilesexceeded", function(file) { alert("File limit 1"); });
           this.on("complete", function(file) {alert(serverReponse);});
           }
        });      
}
$(document).ready(function(){
     addDropzone(1);
     addDropzone(2);
    }
   );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM