Dropzone 过滤文件

Dropzone filtering files

我已经尝试将近一个星期来让它工作我一直在尝试在 dropzone 中使用 accept: 选项归档一些文件我想允许最多 2 个类型为 [=14= 的文件] 当我在 dropzone 中有 2 种类型时,提醒用户她或他达到最大值,同时允许用户上传歌曲。

该代码有些工作,但如果我添加到具有音频 mime 类型的文件,然后在添加图像后它会显示图像警告,但实际上尚未达到限制。

accept: function(file, done){

  // Variables
  var _this = this.getAcceptedFiles(),
      ext = file.name.split('.')
      jpeg = 'image/jpeg',
      png = 'image/png';


  console.log(this.getAcceptedFiles())
  console.log('added')


  // Adding extension to screen for user to see
  $(file.previewElement.childNodes[1]).children().text(ext[ext.length - 1])

  // function in charge of showing and hiding the info box
  // and in charge of displying scrollbar
  box_show();

  // check if file already exist in list
  if (check_the_same(_this, file)){

    done('File ' + file.name + ' already in exist.');

  }else{

    // if file is a image check if we have more than two in list
    if (file.type == jpeg || png){
      if (more_than_2(_this, file)){

        done('Remember just up to 2 images.');
      }else{

        done();
      }

    }else{
      done()
    }
  }
}

函数检查重复文件 :

function check_the_same(_this, file){

  // loop to check if file already exists in added file list
  for(var i = 0; _this.length > i; i++ ){

    // if file name is in already created list show alert    
    console.log($('div#list-container #h').children().length)
    if (file.name == _this[i].name){

      return true
    }

  }

}

检查图像数量的函数 :

function more_than_2(_this, file){

  var num_check = 0;

  for (var i = 0; _this.length > i; i++){
  console.log(_this);

    if (file.type == 'image/jpeg' || 'image/png'){
      console.log(file.type == 'image/jpeg');

      num_check += 1;

      if (num_check >= 2){
        console.log('true')

        return true
      }

    }
  }
}

好吧,你总是在做

file.type == 'image/jpeg' || 'image/png'

在你的循环中,所以如果你总共有 2 个或更多文件并且你试图上传图像,它将失败。

正确的应该是:

_this[i].type == 'image/jpeg' || 'image/png'