简体   繁体   English

将数组输入发送到后端时,Dropzone 插入与相同数量的文件相同数量的值

[英]Dropzone inserting the same number of values as the same number of files when sending array inputs to backend

I have a simple dropbox that uploads files manually when a button is clicked:我有一个简单的保管箱,可以在单击按钮时手动上传文件:

var dropzone = $("#dropzone-form").dropzone({
        autoProcessQueue: false,
        addRemoveLinks: true,
        paramName: "fotos",
        url: url,
        uploadMultiple: true,
        init: function(){
            var dropzone_object = this;



            $('#btn-guardar').on('click', function(e){
                if(checkForm()){
                    if(dropzone_object.files.length > 0){
                        dropzone_object.processQueue();
                    }else{
                      sendWithoutFiles();
                    }

                }
            });


        },
        sending: function(file, xhr, formData) {

            var formValues = $('#form-productos').serializeArray();
            $.each(formValues, function(i, obj){
               formData.append(obj.name, obj.value);
            });


        },
        success: function(file, response){

        },
        error: function(file, errorMessage, xhr){

        }
    });

But I'm having problems when I upload more than one file because I have array inputs inside my form with id form-productos such as this:但是当我上传多个文件时遇到问题,因为我的表单中有数组输入,id form-productos如下:

<input name="emails[]">

or或者

<select name="colors[]"></select>

So, for each file that the dropzone queue has, it appends the input again, the inputs that are NOT array inputs have no problem because the value just gets overwritten again, so let's suppose that I have the following inputs (NOTE THE VALUES):因此,对于 dropzone 队列具有的每个文件,它再次附加输入,不是数组输入的输入没有问题,因为该值再次被覆盖,所以假设我有以下输入(注意值):

<input name="emails[]" value="email1">
<input name="emails[]" value="email2">

And also, let's suppose that I have 2 files in the queue, when I press the button and the data is being sent to PHP, I get the following when I print " $request->emails ":另外,假设队列中有 2 个文件,当我按下按钮并将数据发送到 PHP 时,当我打印“ $request->emails ”时得到以下信息:

array(
   'email1',
   'email2', //After this, since I uploaded two files, I get the same values again:
   'email1',
   'email2'  
)

Reading the dropzone documentation clearly states that this is the correct behaviour:阅读 dropzone文档清楚地表明这是正确的行为:

sending: Called just before each file is sent.发送:在发送每个文件之前调用。 Gets the xhr object and the formData objects as second and third parameters, so you can modify them (for example to add a CSRF token) or add additional data.获取 xhr object 和 formData 对象作为第二个和第三个参数,因此您可以修改它们(例如添加 CSRF 令牌)或添加其他数据。

So, the problem is on my side, what can I do to prevent this when appending to formData ?所以,问题就在我这边,当附加到formData时,我能做些什么来防止这种情况发生?

I solved the issue easily replacing the send event with sendingmultiple , this event should be used in most cases when the uploadMultiple option is set to true :我解决了这个问题,用uploadMultiple轻松替换了send事件, sendingmultiple选项设置为true时,应该在大多数情况下使用此事件:

sendingmultiple: function(files, xhr, formData) {

            var formValues = $('#form-productos').serializeArray();
            $.each(formValues, function(i, obj){
               formData.append(obj.name, obj.value);
            });


        },

The difference is that this event receives a file array as first parameter, in this way, the process inside the event is only done once.不同的是,这个事件接收一个文件数组作为第一个参数,这样,事件内部的过程只执行一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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