Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send additional data with dropzone to the backend

I am trying to send through dropzone a specific -already known- position ID of an image file to the backend, which is going to be uploaded on the server. Although the formData.append() is being used, I see that nothing is appended.Instead just this "FormData {}" shows up.

dropzoneObject.on("sending", function(file, xhr, formData){
    var nameOfFile = $(file.previewElement).find(".dz-filename").text();
    var positionOfFile = fpos;
    //console.log("The file who's being sent is named: "+nameOfFile+" and its position id is: "+positionOfFile);
    formData.append("fpos", fpos);
});

I expect to see in example fpos=16;

like image 612
Sidius Avatar asked Jan 26 '26 17:01

Sidius


1 Answers

Don't know about your particular error, but here is a simple example of how to send additional data with dropzone using jQuery and receiving it with php on the backend.

html:

<form id="myForm" class="dropzone"></form>

js:

Dropzone.autoDiscover = false;
$('.dropzone').dropzone ({
        url: "upload.php",
        init: function() {
            this.on("sending", function(file, xhr, formData){
                formData.append("fpos", 777)
            }),
            this.on("success", function(file, xhr){
                alert(file.xhr.response);
            })
        },
});

The success event is only to demonstrate how to access the response send from the server:

php:

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') 
    {
        echo "RECEIVED ON SERVER: \n";
        echo "FILES: \n";
        print_r($_FILES);
        echo "\$_POST: \n";
        print_r($_POST);
    }

The php simply sends back to client the same data received, just to show where is accessible.

like image 50
wallek876 Avatar answered Jan 28 '26 06:01

wallek876



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!