使用dropzone发送自定义数据。每个文件上传的js

时间:2022-11-23 21:22:57

I am using dropzone in my Code Igniter Project.

我正在我的代码点火器项目中使用dropzone。

With every drag of a file, dropzone creates an ajax request and my files is getting stored on the server too. But now, my requirement is that I want to send additional data (DYNAMIC) alongside the file. With the use of params, Only static datas can be sent, but the data I want to send will be changing everytime.

对于文件的每一次拖动,dropzone都会创建一个ajax请求,我的文件也会存储在服务器上。但是现在,我的要求是,我想在文件旁边发送额外的数据(动态)。使用params,只能发送静态数据,但是我想发送的数据每次都会发生变化。

This is how my code looks:

我的代码是这样的:

<script>
 Dropzone.autoDiscover = false;
  Dropzone.options.attachment = {
        init: function(){
          this.on('removedfile',function(file){
            // console.log('akjsdhaksj');
            var fileName = file.name;
            $.ajax({
              type: 'POST',
              url: "<?php echo BASE_URL.'index.php/admin/mail_actions/deleteFile' ?>",
              data: "id="+fileName,
              dataType: 'html'
            });
          });
        },
        // params: {
        // customerFolder: $('#toValue').substr(0, toValue.indexOf('@')),
        // },
        dictDefaultMessage:"Click / Drop here to upload files",
        addRemoveLinks: true,
        dictRemoveFile:"Remove",
        maxFiles:3,
        maxFilesize:8,
  }

$(function(){

  var uploadFilePath = "<?php echo BASE_URL.'index.php/admin/mail_actions/uploadFile' ?>";
  var myDropzone     = new Dropzone("div#attachment", { url: uploadFilePath});

});
</script>

Anyway I can achieve it?

我能做到吗?

3 个解决方案

#1


105  

I got it. This is what I had to use

我得到了它。这就是我要用的

myDropzone.on('sending', function(file, xhr, formData){
            formData.append('userName', 'bob');
        });

#2


17  

Abhinav has the right and working answer I only want to give a second option to use it in the options object (for example if you have multiple Dropzone sections on one page.)

Abhinav有正确且有效的答案,我只想在options对象中使用它(例如,如果在一个页面上有多个Dropzone区段)。

myDropzone.options.dropzoneDivID = {
    sending: function(file, xhr, formData){
        formData.append('userName', 'Bob');
    }
};

#3


4  

In case you have a nested payload object - e.g. to add a name to your file and your api only accepts something like this

如果您有一个嵌套的有效负载对象——例如,在文件中添加一个名称,而您的api只接受这样的内容

{
    someParameter: {
        image: <my-upload-file>,
        name: 'Bob'
    }
}

your dropzone setup would look like this

您的dropzone设置看起来是这样的。

var myDropzone     = new Dropzone("div#attachment", { 
    url: uploadFilePath,
    paramName: 'someParameter[image]'
});

myDropzone.on('sending', function(file, xhr, formData){
    formData.append('someParameter[image]', file);
    formData.append('someParameter[userName]', 'bob');
});

I only added this as there was no example for nested parameters documented since now.

我只是添加了这一点,因为从现在开始,没有关于嵌套参数的示例。

#1


105  

I got it. This is what I had to use

我得到了它。这就是我要用的

myDropzone.on('sending', function(file, xhr, formData){
            formData.append('userName', 'bob');
        });

#2


17  

Abhinav has the right and working answer I only want to give a second option to use it in the options object (for example if you have multiple Dropzone sections on one page.)

Abhinav有正确且有效的答案,我只想在options对象中使用它(例如,如果在一个页面上有多个Dropzone区段)。

myDropzone.options.dropzoneDivID = {
    sending: function(file, xhr, formData){
        formData.append('userName', 'Bob');
    }
};

#3


4  

In case you have a nested payload object - e.g. to add a name to your file and your api only accepts something like this

如果您有一个嵌套的有效负载对象——例如,在文件中添加一个名称,而您的api只接受这样的内容

{
    someParameter: {
        image: <my-upload-file>,
        name: 'Bob'
    }
}

your dropzone setup would look like this

您的dropzone设置看起来是这样的。

var myDropzone     = new Dropzone("div#attachment", { 
    url: uploadFilePath,
    paramName: 'someParameter[image]'
});

myDropzone.on('sending', function(file, xhr, formData){
    formData.append('someParameter[image]', file);
    formData.append('someParameter[userName]', 'bob');
});

I only added this as there was no example for nested parameters documented since now.

我只是添加了这一点,因为从现在开始,没有关于嵌套参数的示例。