【问题标题】:JQuery : Sending multiple files and parameters got Error: Dropzone already attachedJQuery:发送多个文件和参数得到错误:Dropzone 已附加
【发布时间】:2016-09-28 18:55:49
【问题描述】:

我正在尝试使用 dropzone 从输入字段中将多个文件的值一起发送。不幸的是,我收到一条错误消息,提示 dropzone 已附加。

这是我的代码

 $(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
    });

  });

【问题讨论】:

  • 您正在将 Dropzone 添加到单击事件内的表单元素中,这意味着每次单击该项目时,此代码都会运行。这会在您第一次单击之后为您提供错误消息,因为正如它所说,Dropzone 已经添加到目标元素中。您必须 a) 在 domready 上而不是单击时将 dropzone 添加到表单中,或者 b) 如果您在代码中的其他位置动态添加新字段,则仅将其添加到字段本身,而不是表单。跨度>
  • 如果我使用 a.我的输入字段值不会发送。只有来自 dropzone 的文件,我如何实现文件和输入字段的值相同。
  • 您可以使用带有multiple 属性集的<form> 包括<input type="file" multiple> 元素。用户可以在<input type="file" multiple> 元素中选择多个文件,然后用户可以提交表单,该表单应包含<form> 元素中的文件和其他输入字段。

标签: javascript jquery dropzone.js


【解决方案1】:

Chris Baker 是对的,在这里你要在同一个元素上附加多个 Dropzone,谁会失败。

尝试初始化 dropzone 一次并使用事件“发送”。

// 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));
        ...
    });

});

或者直接在初始化中

// 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));
            ...
        }

    });

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多