我已经实现了JQuery fileupload,但是对于所接受的文件类型有一个小问题:
$('#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的吗?
发布于 2015-07-23 15:25:24
您可以使用RegExp构造函数。
类似于:
acceptFileTypes: new RegExp("^.*\\." + my_condition_lookahead + "[^.]+$", "i"),注意,在使用构造函数符号声明regex时,需要双转义特殊regex元字符。
发布于 2015-07-23 15:26:54
stribizhev的评论很有帮助,我正在创建正则表达式,这是我的代码:
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 + "$|";
}
}在那之后,我的acceptFileTypes看起来像这样:
acceptFileTypes: new RegExp("^\.*\\.(?!" + regex + ")[^.]+$", "i")https://stackoverflow.com/questions/31588693
复制相似问题