Jquery acceptFileTypes Blueimp文件上载
我已经实现了JQuery fileupload,但在接受的文件类型方面有一个小问题:Jquery acceptFileTypes Blueimp文件上载,jquery,regex,file-upload,blueimp,Jquery,Regex,File Upload,Blueimp,我已经实现了JQuery fileupload,但在接受的文件类型方面有一个小问题: $('#fileupload'). url: '/upload/uploaddoc', // URL zum File Upload type: 'POST', dataType: 'json', uploadTemplate: 'template-upload', acceptFileTypes: /^.*\.(?!exe$|lnk$)[^.]+$/i, max
$('#fileupload').
url: '/upload/uploaddoc', // URL zum File Upload
type: 'POST',
dataType: 'json',
uploadTemplate: 'template-upload',
acceptFileTypes: /^.*\.(?!exe$|lnk$)[^.]+$/i,
maxFileSize:allowed_file_size
.......
}
我使用正则表达式来识别不允许的文件类型。但是我想传递一个变量,该变量包含接受的文件类型,就像在maxFileSize中一样,但它似乎不接受列表和字符串
您知道实际传递给acceptFileTypes的是什么吗?您可以使用
RegExp
构造函数
比如:
acceptFileTypes: new RegExp("^.*\\." + my_condition_lookahead + "[^.]+$", "i"),
请注意,在使用构造函数表示法声明正则表达式时,需要对特殊正则表达式元字符进行双转义。Stribizev的评论很有帮助,我正在以友好方式创建正则表达式,这是我的代码:
NotallowedExtensions = ['.lnk', '.exe'];
for(var i= 0; i < NotallowedExtensions.length; i++){
substr = NotallowedExtensions[i].substring(1);//i cut the (.) from the extension here
if(i == NotallowedExtensions.length-1 ){
regex+=substr + "$";
} else {
regex+=substr + "$|";
}
}
您是否尝试过使用
RegExp
?比如说,acceptFileTypes:newregexp(“^.*\.”“+my_condition\u lookahead+”[^.]+$”,“i”),
?这很有帮助,我将把我的解决方案作为答案发布,这样它也可以帮助其他人。
acceptFileTypes: new RegExp("^\.*\\.(?!" + regex + ")[^.]+$", "i")