2016-03-07 70 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。 –

相關問題