如何在jquery中检查文件的多个扩展名

如何在jquery中检查文件的多个扩展名,jquery,kendo-ui,Jquery,Kendo Ui,我的应用程序中有一个剑道上传程序小部件。在“选择”事件中,我必须检查上传的文件是否不是我们希望允许的文件之一,然后停止用户上传文件并显示错误消息。下面的代码是我想到的 function onSelect(e) { var notAllowed = false; $.each(e.files, function (index, value) { var ext = value.extension.toLowerCas

我的应用程序中有一个剑道上传程序小部件。在“选择”事件中,我必须检查上传的文件是否不是我们希望允许的文件之一,然后停止用户上传文件并显示错误消息。下面的代码是我想到的

 function onSelect(e) {
            var notAllowed = false;
            $.each(e.files, function (index, value) {
                var ext = value.extension.toLowerCase();
                if (ext !== '.docx' || '.csv'|| '.doc' || '.docm' || '.dotx' || '.dotm' || '.xls' || '.xlt' || '.xltx' || '.xlsm' || '.xlsx' || '.ppt' || '.pptx' || '.pptm' || '.ppsx' || '.potx' || '.ppsm' || '.tiff' || '.tif' || '.txt')
                {
                    that.viewModel.set("documentMessage", "FileType not allowed.");
                    notAllowed = true;
                }
                if (e.files[0].size / 1024 / 1024 > 50) {
                    that.viewModel.set("documentMessage", "Max FileSize of 50MB exceeded.");
                    notAllowed = true;
                }

                console.log("Name: " + value.name);
                console.log("Size: " + value.size + " bytes");
                console.log("Extension: " + value.extension);
            });
            var breakPoint = 0;
            if (notAllowed == true)
            {
                e.preventDefault();
            }
            };
由于某些原因,无论文件是否为可接受的格式,当用户上载文件时,它总是显示错误消息。当我只检查一种文件格式时,这是完美的。大概是这样的:

 function onSelect(e) {
            var notAllowed = false;
            $.each(e.files, function (index, value) {
                var ext = value.extension.toLowerCase();
                if (ext !== '.docx')
                {
                    that.viewModel.set("documentMessage", "FileType not allowed.");
                    notAllowed = true;
                }
                if (e.files[0].size / 1024 / 1024 > 50) {
                    that.viewModel.set("documentMessage", "Max FileSize of 50MB exceeded.");
                    notAllowed = true;
                }

                console.log("Name: " + value.name);
                console.log("Size: " + value.size + " bytes");
                console.log("Extension: " + value.extension);
            });
            var breakPoint = 0;
            if (notAllowed == true)
            {
                e.preventDefault();
            }
            };
我需要能够检查多个扩展。我在第一段代码中做错了什么? 谢谢

这是:

if (ext !== '.docx' || '.csv'|| '.doc' || '.docm' || '.dotx' || '.dotm' || '.xls' || '.xlt' || '.xltx' || '.xlsm' || '.xlsx' || '.ppt' || '.pptx' || '.pptm' || '.ppsx' || '.potx' || '.ppsm' || '.tiff' || '.tif' || '.txt')
这不是你真正想要的。您的第一个条件是可以的(
ext!='.docx'
),但以下只是计算为
true
的字符串,这就是始终满足这些条件的原因。在控制台中尝试此操作
Boolean(“false”)
。由于参数字符串的类型而不是其内容,它将计算为
true
。字符串在javascript中为true。这将产生与您的
相同的结果,如果

if ('false') { console.log("I'm truthy") }
if (false) { console.log("I'm truthy") }
所以,让你的所有条件都像第一个:

if (ext !== '.docx' || ext !== '.csv'|| ext !== '.doc' /* and so on */
为了提高可读性甚至性能,您可以更改条件以检查字符串数组,只设置一个条件而不是所有这些条件:

var forbiddenExtensions = ['.docx', '.csv', '.doc'];
if (forbiddenExtensions.indexOf(ext) > -1) {
    // Error
}

更好的是:
if(['.docx'、'.csv'、'.docm'、'.docm'、'.dotx'、'.dotm'、'.xls'].indexOf(ext)>-1){blablabla}
,可能会将数组保存在某个配置位置^^非常感谢您给出正确的答案并进行解释!!:)这很管用!仅供参考,最好在服务器端进行块检查。JS i很容易破解,如果有人想造成伤害,很可能他们正在寻找这样的漏洞。