【发布时间】:2020-07-18 17:38:54
【问题描述】:
我正在使用 dropzone 上传文件,这些文件与后端数据库中的一些元数据一起保存。 当用户尝试上传大量文件时,我想检查他是否已经上传了具有此名称的文件,并通过警报警告他,并选择继续或停止。
所以我禁用了autoProcessQueue。
我也在监听addedfile事件,我得到了名字,我执行了一个ajax来检查它是否存在于数据库中并返回true或false,很好。
假设用户尝试上传 40 个已经存在的文件,我不想要 40 个警告该文件已经存在,我想要一个通知打印所有 40 个文件名。
我一直在寻找 addedfile 但对于多个文件,我没有找到解决方案。
这是我现在的代码 这就是我卡住的地方 我如何知道我何时检查了每个文件?
$(document).ready(function() {
@if(isset($checkRoute))
function filenameExists(name) {
$.ajax({
type: 'GET',
url: '{{ $checkRoute }}',
data: {
name: name
}
})
.done(function(res) {
// returns true or false
return res
})
.fail(function(err) {
console.log(err)
})
}
@endif
let existingFilenames = []
let fileCount = 0
$('#{{ $element_id }}').dropzone({
url: "{{ $upload }}", // Set the url for your upload script location
paramName: "documents", // The name that will be used to transfer the file
maxFiles: 100,
maxFilesize: 100, // MB
parallelUploads: 100,
timeout: 240000,
addRemoveLinks: true,
acceptedFiles: "application/msword, application/vnd.ms-excel, application/pdf, image/*, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, image/*",
uploadMultiple: true,
headers: {
'X-CSRF-TOKEN': "{{ csrf_token() }}"
},
init: function() {
@if(isset($checkRoute))
this.on('addedfile', function(file) {
console.log(this.getAcceptedFiles())
fileCount++
if (filenameExists(file.name)) {
console.log(file.name)
existingFilenames.push(file.name)
}
})
@endif
},
autoProcessQueue: false,
sendingmultiple: function (files) {
// Begin loading
KTApp.blockPage({
overlayColor: '#000000',
type: 'v2',
state: 'success',
message: '{{ __('documents_uploading') }}'
});
},
queuecomplete: function () {
// End loading
KTApp.unblockPage();
$.notify({
// options
message: '{{ __('documents_upload_success') }}'
}, {
// settings
type: 'success',
placement: {
from: "top",
align: "center"
},
animate: {
enter: 'animated fadeInDown',
exit: 'animated fadeOutUp'
},
});
window.location.replace("{{ $redirect }}");
}
});
});
我关心的另一件事是我将如何在按下通知中的按钮时处理队列。
【问题讨论】:
-
好吧,如果你将所有警告推送到一个数组中并一起打印?
标签: javascript dropzone.js dropzone