45

I am using dropzone in my Code Igniter Project.

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.

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?

4 Answers 4

133

I got it. This is what I had to use

myDropzone.on('sending', function(file, xhr, formData){
    formData.append('userName', 'bob');
});
Sign up to request clarification or add additional context in comments.

12 Comments

i tried using this but no luck..and where to add this code? in init ?
How have you defined your dropzone? Without a snippet its hard to answer. Create a fiddle
it is sending but not understanding the way it is sending.Content-Disposition: form-data; name="name" sss Content-Disposition: form-data; name="description"
Try remove the sending method when creating the dropzone instance, and bind the sending method using on
what is username and bob ? is those are form fields ?
|
30

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.)

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

2 Comments

I gave your answer an upvote since it shows that there is more than one way to implement this feature
how do you put variable outside dropzone to the form data append?
13

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

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

your dropzone setup would look like this

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.

Comments

2

i was using JQuery and this is how worked for me :

           $("#dZUpload").dropzone({
                url: "ajax/upload_img_ben.php",
                success: function (file, response) {
                    var imgName = response;
                    file.previewElement.classList.add("dz-success");
                    console.log("Successfully uploaded :" + imgName);
                },
                error: function (file, response) {
                    file.previewElement.classList.add("dz-error");
                },
                sending: function(file, xhr, formData){
                    formData.append('id', '5');
                }
            });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.