admin管理员组文章数量:1323200
I'm using dropzone to upload images to gallery. I'm submiting by button. Is it possible to prevent adding same file twice? I'm not really sure if checking name or name and size. Here's my code:
<script>
var i = 0;
Dropzone.options.myDropzone = {
init: function() {
this.on("addedfile", function(file) {
i=++i;
check filenames and filesizes of other files here
----------->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("plete", function(file) {
myDropzone.removeFile(file);
});
this.on("errormultiple", function(files, response) {
});
},
autoProcessQueue: false,
previewsContainer: ".dropzone",
uploadMultiple: true,
parallelUploads: 25,
maxFiles: 25,
};
</script>
I'm using dropzone to upload images to gallery. I'm submiting by button. Is it possible to prevent adding same file twice? I'm not really sure if checking name or name and size. Here's my code:
<script>
var i = 0;
Dropzone.options.myDropzone = {
init: function() {
this.on("addedfile", function(file) {
i=++i;
check filenames and filesizes of other files here
----------->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("plete", function(file) {
myDropzone.removeFile(file);
});
this.on("errormultiple", function(files, response) {
});
},
autoProcessQueue: false,
previewsContainer: ".dropzone",
uploadMultiple: true,
parallelUploads: 25,
maxFiles: 25,
};
</script>
Share
Improve this question
edited Nov 20, 2014 at 15:56
jesus6402
asked Nov 20, 2014 at 14:54
jesus6402jesus6402
411 silver badge8 bronze badges
3 Answers
Reset to default 6Add these simple lines of code:
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);
}
}
}
});
this.on("addedfile", function (file) {
if (this.files.length) {
var i, len, pre;
for (i = 0, len = this.files.length; i < len - 1; i++) {
if (this.files[i].name == file.name) {
alert("The Doc.: " + file.name + " is already registered.")
return (pre = file.previewElement) != null ? pre.parentNode.removeChild(file.previewElement) : void 0;
}
}
}
});
Checking the file name and size should be fine. I already tested that and it had worked almost fine for me and haven't crossed any issues with it.
The original thread I landed in was a Git issue #639 where a munity member posted his solution for name and size verification.
That trick was also mentioned in a similar post answer.
本文标签: javascriptDropzone prevent addfile twiceStack Overflow
版权声明:本文标题:javascript - Dropzone: prevent addfile twice - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742096156a2420561.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论