简体   繁体   中英

JQuery : Sending multiple files and parameters got Error: Dropzone already attached

I'm trying to send multiple files together of of value from input fields using dropzone. Unfortunately I got an error says dropzone already attached.

Here is my code

 $(document).on('click','#addContestant',function(e){
 e.preventDefault();
 var random_gender = $('#randomGender').val();
    var contestant_name = $('#contestant_name').val();
    var contestant_lastName = $('#contestant_lastName').val();
    var conAge = $('#conAge').val();
    var hAddress = $('#hAddress').val();
    var email_add = $('#email_add').val();
    var conContactNum = $('#conContactNum').val();
    var conDesc = $('#conDesc').val();
    var hidden_gender = $('#hidden_gender').val();
    var conId_hidden = $('#conId_hidden').val(); // the contestant prima id

    var param = "?event_id="+encodeURIComponent(event_id)+
                "&contestant_name="+encodeURIComponent(contestant_name)+
                "&contestant_lastName="+encodeURIComponent(contestant_lastName)+
                "&conAge="+encodeURIComponent(conAge)+
                "&hAddress="+encodeURIComponent(hAddress)+
                "&email_add="+encodeURIComponent(email_add)+
                "&conContactNum="+encodeURIComponent(conContactNum)+
                "&conDesc="+encodeURIComponent(conDesc)+
                "&conId_hidden="+encodeURIComponent(conId_hidden)+
                "&hidden_gender="+encodeURIComponent(hidden_gender)+
                "&random_gender="+encodeURIComponent(random_gender)+
                "&multipleImage="+encodeURIComponent(multipleImage);

    Dropzone.autoDiscover = false;
    var myDropzone = new Dropzone('form#my-awesome-dropzone', {
        url : '../ajax/ajax_add/ajax_addNEWContestant.php?'+param,
        maxFilesize: 3.0, 
        maxFiles: 4,
        parallelUploads: 10000,
        uploadMultiple: true,
        autoProcessQueue: false
    });

  });

Chris Baker is right, here you going to attach multiple Dropzone on same element, who will fails.

Try to init dropzone once and use event 'sending'.

// Dropzone config
Dropzone.autoDiscover = false;

// To run when dom is ready
$(document).ready(function(){

    // Manual Dropzone init
    var myDropzone = new Dropzone('form#my-awesome-dropzone', {
        url : '../ajax/ajax_add/ajax_addNEWContestant.php',
        maxFilesize: 3.0,
        ...
    });

    // Add dynamic event on sending
    myDropzone.on('sending', function(file, xhr, formData){
        // Fetch values to send
        var contestant_name = $('#contestant_name').val();
        ...

        // Add it to request data send by Dropzone
        formData.append('event_id', encodeURIComponent(event_id));
        formData.append('contestant_name', encodeURIComponent(contestant_name));
        ...
    });

});

or directly in initialisation

// Dropzone config
Dropzone.autoDiscover = false;

// To run when dom is ready
$(document).ready(function(){

    // Manual Dropzone init
    var myDropzone = new Dropzone('form#my-awesome-dropzone', {
        url : '../ajax/ajax_add/ajax_addNEWContestant.php',
        maxFilesize: 3.0,
        ...
        sending: function(file, xhr, formData){
            // Fetch values to send
            var contestant_name = $('#contestant_name').val();
            ...

            // Add it to request data send by Dropzone
            formData.append('event_id', encodeURIComponent(event_id));
            formData.append('contestant_name', encodeURIComponent(contestant_name));
            ...
        }

    });

});

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