判断上传文件类型的js代码

发布时间:2019-09-29编辑:脚本学堂
用于判断与检测上传文件类型的一段js代码,js数组中保存允许上传的诸多文件类型,除此之外的,禁止上传。供大家学习参考。

完整代码如下。
 

复制代码 代码示例:

//判断文件类型
function changeType(objFile) {
    var objtype=objFile.substring(objFile.lastIndexOf(".")).toLowerCase();
    var fileType=new Array(".ad",".adprototype",".asa",".asax",".ascx",".ashx",".asmx",".asp",".aspx",".axd",".browser",".cd",".cdx",".cer",".compiled",
".config",".cs",".csproj",".dd",".exclude",".idc",".java",".jsl",".ldb",".ldd",".lddprototype",".ldf",".licx",".master",
".mdb",".mdf",".msgx",".refresh",".rem",".resources",".resx",".sd",".sdm",".sdmdocument",".shtm",".shtml",
".sitemap",".skin",".soap",".stm",".svc",".vb",".vbproj",".vjsproj",".vsdisco",".webinfo");

    for(var i=0; i<fileType.length; i++){
        if(objtype==fileType[i])
        {
             return false;
             break;
        }
    }
    return true;
}

//设置上传按钮是否启用
function changeAble(objFile)
{
   document.getElementById("fup").disabled=true;
   if(!changeType(objFile))
   {
       alert("请不要上传非法类型的文件!");
       document.getElementById("fup").disabled=true;
   }
   else
   {
        document.getElementById("fup").disabled=false;
   }
}

编辑推荐:php检测文件头信息 判断文件类型

#--->>常用js验证代码大全(Email、手机号码、身份证号码、文件类型等)