【问题标题】:Dropzone filtering filesDropzone过滤文件
【发布时间】:2017-05-30 17:55:50
【问题描述】:

我已经尝试了将近一个星期来使它工作我一直在尝试使用 dropzone 中的 accept: 选项来归档一些文件我想允许最多 2 个类型为 'image/png' and 'image/jpeg' 的文件,当我到达在 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
      }

    }
  }
}

【问题讨论】:

    标签: javascript jquery dropzone.js


    【解决方案1】:

    嗯,你总是这样做

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

    在您的循环中,因此如果您总共有 2 个或更多文件并且您尝试上传图片,它将失败。

    正确的应该是:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-04
      • 2018-03-06
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多