2016-03-04 53 views
0

我有以下代码:如何检查在dropzone中是否已有图像?

CODE JS:

Dropzone.autoDiscover = false; 
var myDropzone = new Dropzone("div#myDrop", { 
    url    : "<?php echo $this->serverUrl() . '/profile/ajax/dzupload'; ?>", 
    paramName  : 'profilepicture', 
    acceptedFiles : "image/*", 
    maxFiles  : 1, 
    addRemoveLinks : true, 
    init   : function() { 
     this.on("success", function(file, responseText) { 

      $('#profilePicture').attr("value", responseText.filename); 
      console.log(responseText); 
     }); 

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

     this.on("removedfile", function (file){ 
      $.ajax({ 
       method: 'POST', 
       url: '<?php echo $this->serverUrl() . '/profile/ajax/dzdelete' ?>', 
       data: { 
        area : 'company-profile', 
        name : $('#profilePicture').attr('value') 
       }, 
       success: function(result){ 
        $('#profilePicture').attr("value", '') 
        console.log(result); 
       } 
      }) 
     }); 
    } 
}); 

     var fileName = $('#profilePicture').val(); 
     var mockFile = { name: fileName, size: 12345 }; 
     myDropzone.options.addedfile.call(myDropzone, mockFile); 
     myDropzone.options.thumbnail.call(myDropzone, mockFile, "<?php echo $this->serverUrl().'/public/profile/'.'usr'.$practitionerInfo->user_id.'/picture/'.$practitionerInfo->profilepicture ?>"); 

我想检查是否有已加载图像......如果不是有那么让你添加其他图像,直到我删除以前的

我试着下面的代码,但它不工作

if(!myDropzone.file.length()){ 
    //allow to upload file 
}else{ 
    alert("please remove existing file"); 
} 

我该怎么做?我希望我能够解释得很好。

编辑:

如果添加此代码0,但画面增收默认,应该是一个,对不对?

console.log(myDropzone.files.length) //return 0 

回答

0

你几乎拥有它。由于myDropzone.files是一个数组,因此myDropzone.files.length会告诉您是否有任何选定的文件。

if (!myDropzone.files || !myDropzone.files.length) { 
    // allow to upload file 
} else { 
    alert("please remove existing file"); 
} 

我包括一个!myDropzone.files检查,因为我敢肯定不是100%时,这个属性被初始化,所以它可能当你尝试检查它的长度是不确定的。

+0

请看我编辑的帖子 –

+0

@ D.Cristi'files'数组只包含Dropzone控件中的文件。如果您想阻止用户上传第二张个人资料照片,则可以在您的成功处理程序中禁用上传内容,或者使用新照片替换现有的个人资料照片。 – twernt