acceptFileTypes Blueimp 文件上传

acceptFileTypes Blueimp fileupload

我已经实现了 JQuery 文件上传,但是接受的文件类型有一个小问题:

$('#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"),

请注意,在使用构造函数表示法声明正则表达式时,您需要对特殊的正则表达式元字符进行两次转义。

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")