Javascript Dropzone筛选文件
我已经尝试了将近一周的时间来实现这一点,我一直在尝试在dropzone中使用Javascript Dropzone筛选文件,javascript,jquery,dropzone.js,Javascript,Jquery,Dropzone.js,我已经尝试了将近一周的时间来实现这一点,我一直在尝试在dropzone中使用accept:选项来归档一些文件,我想允许最多2个文件的类型为“image/png”和“image/jpeg”,当我在dropzone中有2个这样的类型时,提醒用户她或他达到了最大值,同时允许用户上传歌曲 代码在某种程度上起作用,但如果我添加到具有音频mime类型的文件中,那么在添加图像后,它会显示图像警告,但实际上尚未达到该限制 accept: function(file, done){ // Variables
accept:
选项来归档一些文件,我想允许最多2个文件的类型为“image/png”和“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
}
}
}
}
检查图像数量的函数:
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'