我正在使用dropzone将图像上传到图库。我按按钮提交。是否可以防止两次添加相同文件?我不太确定是否要检查名称或名称和大小。这是我的代码:

<script>
      var i = 0;
      Dropzone.options.myDropzone = {
        init: function() {
            this.on("addedfile", function(file) {
            i=++i;

在这里检查文件名和其他文件的文件大小
----------->if(){
                myDropzone.removeFile(file);
            }


            var inputs = Dropzone.createElement('<div class="dz-image-metadata"><label for="'+i+'_title">Nazov</label><input type="hidden" name="'+i+'_filename" value="'+file.name+'"><input type="text" name="'+i+'_title" value="'+file.name+'" /><label for="">Popis</label><input type="text" name="'+i+'_desc"></div>');
            file.previewElement.appendChild(inputs);
            var removeButton = Dropzone.createElement("<button class=\"dz-button\">Vymazať</button>");
            var _this = this;
            removeButton.addEventListener("click", function(e) {
              e.preventDefault();
              e.stopPropagation();
              i=--i;
              _this.removeFile(file);
            });
            file.previewElement.appendChild(removeButton);
          });
          var myDropzone = this;
           $("#submit-all").click(function (e) {
            e.preventDefault();
            e.stopPropagation();
            myDropzone.processQueue();
            i=0;
        }
        );

         this.on("successmultiple", function(files, response) {
           console.log(response);
        });

         this.on("complete", function(file) {
            myDropzone.removeFile(file);
        });

        this.on("errormultiple", function(files, response) {
             });
        },
        autoProcessQueue: false,
        previewsContainer: ".dropzone",
        uploadMultiple: true,
        parallelUploads: 25,
        maxFiles: 25,
      };
    </script>

最佳答案

添加以下简单的代码行:

myDropzone.on("addedfile", function(file) {
    if (this.files.length) {
        var _i, _len;
        for (_i = 0, _len = this.files.length; _i < _len - 1; _i++) // -1 to exclude current file
        {
            if(this.files[_i].name === file.name && this.files[_i].size === file.size && this.files[_i].lastModified.toString() === file.lastModified.toString())
            {
                this.removeFile(file);
            }
        }
    }
});

关于javascript - Dropzone:两次阻止addfile,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27042588/

10-13 08:46