使用 dropzone 向后端发送附加数据

2022-01-15 00:00:00 xmlhttprequest jquery php ajax dropzone.js

我正在尝试通过 dropzone 将图像文件的特定 - 已经知道 - 位置 ID 发送到后端,该 ID 将上传到服务器上.虽然正在使用 formData.append(),但我看到没有附加任何内容.而只是显示了这个FormData {}".

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);
});

我希望在示例中看到 fpos=16;

I expect to see in example fpos=16;

推荐答案

不知道你的具体错误,但这里有一个简单的例子,说明如何使用 jQuery 使用 dropzone 发送附加数据并在后端使用 php 接收它.

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: 
";
        echo "FILES: 
";
        print_r($_FILES);
        echo "$_POST: 
";
        print_r($_POST);
    }

php 只是将接收到的相同数据发送回客户端,只是为了显示可访问的位置.

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

相关文章