gpt4 book ai didi

javascript - 拖放区 : prevent addfile twice

转载 作者:行者123 更新时间:2023-11-29 18:11:27 29 4
gpt4 key购买 nike

我正在使用 dropzone 将图像上传到画廊。我正在通过按钮提交。是否可以防止两次添加相同的文件?我不太确定是检查名称还是名称和大小。这是我的代码:

<script>
var i = 0;
Dropzone.options.myDropzone = {
init: function() {
this.on("addedfile", function(file) {
i=++i;

在这里检查其他文件的文件名和文件大小

----------->if(){
myDropzone.removeFile(file);
}


var inputs = Dropzone.createElement('<div class="dz-image-metadata"><label for="'+i+'_title">Nazov</label><input type="hidden" name="'+i+'_filename" value="'+file.name+'"><input type="text" name="'+i+'_title" value="'+file.name+'" /><label for="">Popis</label><input type="text" name="'+i+'_desc"></div>');
file.previewElement.appendChild(inputs);
var removeButton = Dropzone.createElement("<button class=\"dz-button\">Vymazať</button>");
var _this = this;
removeButton.addEventListener("click", function(e) {
e.preventDefault();
e.stopPropagation();
i=--i;
_this.removeFile(file);
});
file.previewElement.appendChild(removeButton);
});
var myDropzone = this;
$("#submit-all").click(function (e) {
e.preventDefault();
e.stopPropagation();
myDropzone.processQueue();
i=0;
}
);

this.on("successmultiple", function(files, response) {
console.log(response);
});

this.on("complete", function(file) {
myDropzone.removeFile(file);
});

this.on("errormultiple", function(files, response) {
});
},
autoProcessQueue: false,
previewsContainer: ".dropzone",
uploadMultiple: true,
parallelUploads: 25,
maxFiles: 25,
};
</script>

最佳答案

添加这些简单的代码行:

myDropzone.on("addedfile", function(file) {
if (this.files.length) {
var _i, _len;
for (_i = 0, _len = this.files.length; _i < _len - 1; _i++) // -1 to exclude current file
{
if(this.files[_i].name === file.name && this.files[_i].size === file.size && this.files[_i].lastModified.toString() === file.lastModified.toString())
{
this.removeFile(file);
}
}
}
});

关于javascript - 拖放区 : prevent addfile twice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27042588/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com