Send custom data with dropzone.js on each File Upload

前端 未结 4 1369
孤独总比滥情好
孤独总比滥情好 2020-12-23 19:38

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

相关标签:
4条回答
  • 2020-12-23 20:12

    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.

    0 讨论(0)
  • 2020-12-23 20:15

    I got it. This is what I had to use

    myDropzone.on('sending', function(file, xhr, formData){
        formData.append('userName', 'bob');
    });
    
    0 讨论(0)
  • 2020-12-23 20:19

    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');
        }
    };
    
    0 讨论(0)
  • 2020-12-23 20:23

    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');
                    }
                });
    
    0 讨论(0)
提交回复
热议问题