如何将其他参数添加到XHR对象(在Dropzone.JS中)?

MK *_*ung 0 javascript xmlhttprequest dropzone.js

我正在尝试将我的照片与参数中发送的标签(逗号分隔的字符串)一起上传tagname.

sending传递给Dropzone.JS 的选项允许我在发送请求之前获取XHR对象.

Dropzone.options.uploadDropzone({
    // ...
    sending: function(file, xhr, formData){
        // but how to add my tags string to the params?
        // any methods like setting the header: 
        // xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest")?
    }
})
Run Code Online (Sandbox Code Playgroud)

小智 5

在javascript中

    var xhr = new XMLHttpRequest();
    xhr.open("POST", "FileUploadHandler.ashx");
    var fd = new FormData();
    fd.append("sFileTitle", document.getElementById('txtFileTitle').value);

    xhr.send(fd);
Run Code Online (Sandbox Code Playgroud)

因此,您可以使用键和值对在表单数据中附加数据

var fd = new FormData();
    fd.append("sFileTitle", document.getElementById('txtFileTitle').value);
    Dropzone.options.uploadDropzone({
        // ...
        sending: function(file, xhr, fd){
            // but how to add my tags string to the params?
            // any methods like setting the header: 
            // xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest")?
        }
    })
Run Code Online (Sandbox Code Playgroud)

  • 大!关键是要改变`formdata`,而不是`xhr` obj !! 我刚刚在`sending`中添加了`formData.append({'tagname':'example'})`,它就像一个魅力!谢谢! (2认同)