将 Dropzone 的一个文件发送到多个 url
Send one file of Dropzone to multiple urls
我已经使用 Dropzone 一段时间了,而且效果很好。虽然,我不得不改变我的方法。我曾经将文件发送到一个 url(在 Dropzone 中通常如此),但是,我需要将它发送到多个 url(同一个文件)。我找不到答案。有人知道原因吗?
我认为 dropzone 中没有内置功能可以做到这一点,但是您可以为每增加一个 url 发送一个额外的 xmlhttprequest
,您可以发送第二个请求获取拖放区文件对象的事件。
这里是默认上传成功时发送它的最低限度示例:
js:
Dropzone.options.myDropzone = {
// This is the default dropzone url in case is not defined in the html
url: 'upload.php',
init: function() {
this.on('success', function(file){
// Just to see default serve response on console
console.log(file.xhr.responseText);
// Creat a new xmlhttprequest with the second url
secondRequest = new XMLHttpRequest();
secondRequest.open('POST', 'upload2.php', true);
// Just to see second server response on success
secondRequest.onreadystatechange = function () {
if(secondRequest.readyState === XMLHttpRequest.DONE && secondRequest.status === 200) {
console.log(secondRequest.responseText);
}
};
// Create a new formData and append the dropzone file
var secondRequestContent = new FormData();
secondRequestContent.append('file', file, file.name);
// Send the second xmlhttprequest
secondRequest.send(secondRequestContent);
});
}
};
我已经使用 Dropzone 一段时间了,而且效果很好。虽然,我不得不改变我的方法。我曾经将文件发送到一个 url(在 Dropzone 中通常如此),但是,我需要将它发送到多个 url(同一个文件)。我找不到答案。有人知道原因吗?
我认为 dropzone 中没有内置功能可以做到这一点,但是您可以为每增加一个 url 发送一个额外的 xmlhttprequest
,您可以发送第二个请求获取拖放区文件对象的事件。
这里是默认上传成功时发送它的最低限度示例:
js:
Dropzone.options.myDropzone = {
// This is the default dropzone url in case is not defined in the html
url: 'upload.php',
init: function() {
this.on('success', function(file){
// Just to see default serve response on console
console.log(file.xhr.responseText);
// Creat a new xmlhttprequest with the second url
secondRequest = new XMLHttpRequest();
secondRequest.open('POST', 'upload2.php', true);
// Just to see second server response on success
secondRequest.onreadystatechange = function () {
if(secondRequest.readyState === XMLHttpRequest.DONE && secondRequest.status === 200) {
console.log(secondRequest.responseText);
}
};
// Create a new formData and append the dropzone file
var secondRequestContent = new FormData();
secondRequestContent.append('file', file, file.name);
// Send the second xmlhttprequest
secondRequest.send(secondRequestContent);
});
}
};