javascript - Second dropzone doesn't listen to events -
i using dropzonejs file uploading , stumbled upon issue of don't know answer to.
i have 2 dropzones: 1 multiple files , 1 single file. single file version meant replace uploaded file different one, hence allows 1 file uploaded while first 1 allows multiple.
the first dropzone works noticed second dropzone doesn't listen 'sending' event , 'success' event. listen 'error' event , 'accept' event. first dropzone has same events yet works fine dropzone, know how fix this?
the code of interest here below.
dropzones
dropzone.options.requestfileupload = { init: function(){ this.on('error', function (file, response) { console.log("error"); }), this.on('sending', function (file, xhr, formdata) { if (file.accepted) formdata.append('id', 'uploadfile'); }), this.on('success', function (file, response) { console.log("success!"); }) }, accept: function(file, done){ console.log('file accepted!'); }, acceptedfiles: ".dxf", previewscontainer: false, paralleluploads: 5, createimagethumbnails: false } dropzone.options.requestfileedit = { init: function(){ this.on('error', function (file, response) { //fired }), this.on('sending', function (file, xhr, formdata) { // not fired console.log("sending"); if (file.accepted) formdata.append('id', 'uploadfile'); }), this.on('success', function(file, response){ // not fired console.log('success'); }) }, accept: function(file, done){ //fired }, maxfiles: 1, acceptedfiles: ".dxf", previewscontainer: false, createimagethumbnails: false }
<script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.1.1/min/dropzone.min.js"></script> <body> <!-- markup --> <form data-target="target_i_set_using_js" action="myaction.php" class="dropzone" id="requestfileedit"/> <!-- other markup --> <form action="myaction.php" class="dropzone" id="requestfileupload"/> </body>
i don't know why problem happens, when played code litte bit found init
method of 1 of dropzones never called.
this solution dropzones initialized programmatically(docs) , in case can see both dropzones handling events fine(well in post see error
handler triggered think work fine in real world scenario)
dropzone.autodiscover = false; var dzrequestfileedit = new dropzone("div#requestfileedit", { url: "/file/post", init: function() { console.log('initializing requestfileedit ...'); this.on('error', function(file, response) { console.log("error requestfileedit"); }); this.on('sending', function(file, xhr, formdata) { console.log('sending requestfileedit ...'); if (file.accepted) formdata.append('id', 'uploadfile'); }); this.on('success', function(file, response) { console.log("success!"); }); }, accept: function(file, done) { console.log('file accepted!'); }, acceptedfiles: ".dxf", previewscontainer: false, paralleluploads: 5, createimagethumbnails: false }); var dzrequestfileupload = new dropzone("div#requestfileupload", { url: "/file/post", init: function() { console.log('initializing requestfileupload ...'); this.on('error', function(file, response) { console.log("error requestfileupload"); }); this.on('sending', function(file, xhr, formdata) { console.log('sending requestfileupload ...'); if (file.accepted) formdata.append('id', 'uploadfile'); }); this.on('success', function(file, response) { console.log("success!"); }); }, accept: function(file, done) { console.log('file accepted!'); }, acceptedfiles: ".dxf", previewscontainer: false, paralleluploads: 5, createimagethumbnails: false });
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.1.1/min/dropzone.min.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.1.1/min/basic.min.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.1.1/min/dropzone.min.js"></script> <body> <!-- markup --> <div style="float:left;width:150px"> <h3 style="margin:0">file edit</h3> <div class="dropzone" id="requestfileedit"></div> </div> <!-- other markup --> <div style="float:left;width:150px;margin-left:10px;"> <h3 style="margin:0">file upload</h3> <div class="dropzone" id="requestfileupload"></div> </div> </body>
let me know if works , if not clear.
Comments
Post a Comment