如何检查是否有图像添加到dropzone?

时间:2016-03-07 09:53:44

标签: javascript jquery html dropzone.js

我有这个样本:

link

代码HTML:

<div class="dropzone dz-clickable" id="myDrop">
        <div class="dz-default dz-message" data-dz-message="">
                <span>Upload or drag patient photo here</span>
        </div>
</div>

CODE JS:

    Dropzone.autoDiscover = false;
    var myDropzone = new Dropzone("div#myDrop", {
    addRemoveLinks: true,
    url: "#",
    maxFiles:1,
init: function() {

      this.on("maxfilesexceeded", function(file) {
        alert("You are not allowed to chose more than 1 file!");
            this.removeFile(file);

      });

}   
});



var fileName = $('#profilePicture').val();
var mockFile = { name: fileName, size: 12345 };
myDropzone.options.addedfile.call(myDropzone, mockFile);
myDropzone.options.thumbnail.call(myDropzone, mockFile, "https://lh3.googleusercontent.com/-eubcS91wUNg/AAAAAAAAAAI/AAAAAAAAAL0/iE1Hduvbbqc/photo.jpg?sz=104");

我想要做的是检查是否已经加载了图像。

例如:

if(is an image loaded from the first?)
{
   alert("you have already image, delete it first");
}else{
   alert("you can upload image now");
}

基本上他们不希望用户在已经加载的情况下再放一张图片。

正如您在我的示例中所看到的,第一个加载图像...如果您想加载另一个图像,请允许我们(我不想要它)。

这有什么办法可以限制吗?

提前致谢!

2 个答案:

答案 0 :(得分:0)

DropZone提供方法enable() and disable(),您可以使用它来控制它的使用。

您可以使用drop or addedFile事件来调用此函数:

init: function() {
  this.on("addedFile", function(file) {
    // do what you want with the added file
    this.disable();  // disable the dropzone to prevent more files
  });
}

答案 1 :(得分:0)

对我而言,如果图片已经在dropzone中上传,则不允许我添加更多内容。

 this.on("addedfile", function (file) {
                /* 
                Valid only in the dropzone . If a repetitive document shows ALERT and the previous item will disappear.(Sorry my English).
                */
                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 && this.files[i].size == file.size && this.files[i].lastModifiedDate.toString() == file.lastModifiedDate.toString()) {
                            alert("You have already image, delete it first")
                            return (pre = file.previewElement) != null ? pre.parentNode.removeChild(file.previewElement) : void 0;
                        }
                    }
                }
            });