2016-03-07 73 views
0

我有这样的例子:如何检查是否有图像添加到dropzone?

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"); 
} 

基本上他们不想让用户可以把另一张照片,如果有已经加载。

正如你可以在我的例子中看到的,第一个加载图像...如果你想加载另一个,允许我们(我不想要它)。

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

在此先感谢!

+0

在删除文件时触发事件时删除或禁用dropzone。 –

+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 
    }); 
} 
+0

这里检查图像是否存在? 我感兴趣的是他们如何改变病情 –

0

工作对我来说,如果图像是在悬浮窗已上载,它不会让我增加更多。

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; 
         } 
        } 
       } 
      }); 
+0

Espero que de algo te halla ayudado。 –

相关问题