我正在使用 dropzone.js 将某些文件上传到我的服务器。我遇到的问题是,有时服务器无法跟上连接并拒绝某些上传,因此它们会失败并用 x 标记为红色。我想在一定时间后自动重试,或者至少让用户能够手动重启它。
dropzone.js 中是否有一个已实现的功能,一种足够简单的方法来为我自己实现它,或者是否有更好的工具来通过拖放、预览、ajax 等进行这些上传...?
最佳答案
我的解决方案是不更改 Dropzone 库并且只有 4 行长。我尝试文件上传两次,因为第一次失败的请求设置了一个新的基于 cookie 的 CSRF token :
var isFirstTry = true;
myDropzone = new Dropzone(document.body, {
init: function() {
this.on("sending", function(file, xhr, formData) {
xhr.setRequestHeader("X-CSRF", Cookies.get('CSRF'));
});
},
error: function(file, errorMessage, xhr){
if (errorMessage && errorMessage.status === 405 && file && isFirstTry) {
isFirstTry = false;
//remove item from preview
this.removeFile(file)
//duplicate File objet
new File([file], file.name, { type: file.type });
this.uploadFile(file);
}
},
// other configs
});
在 dropzone error
事件之后,无论结果如何,dropzone 都会触发 complete
事件。在完成 dropzone
时,将状态元素设置为 complete
。这隐藏了进度条。要防止此行为,请复制 File
对象。这会阻止 complete hook
处理新的预览元素。
关于javascript - 如果 ajax 失败,Dropzone.js 重试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20447926/