【问题标题】:How to check if there is already an image in dropzone?如何检查dropzone中是否已经有图像?
【发布时间】:2016-03-04 14:28:12
【问题描述】:

我有以下代码:

代码 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但是图片加了receive default应该是1吧?

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

【问题讨论】:

    标签: javascript jquery html dropzone.js


    【解决方案1】:

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

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

    我包含了!myDropzone.files 检查,因为我不能 100% 确定何时初始化此属性,因此当您尝试检查其长度时它可能未定义。

    【讨论】:

    • @D.Cristi files 数组仅包含 Dropzone 控件中的文件。如果您想阻止用户上传第二张个人资料图片,您可以在成功处理程序中禁用上传或将现有的个人资料图片替换为新的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    • 2012-11-18
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多