//JavaScript determines file type based on filenamevarImgext =NewArray (". png", ". jpg", ". jpeg", ". bmp", ". gif");//the suffix name of the picture filevarDocext =NewArray (". Doc", ". docx");//the suffix name of the Word filevarXlsext =NewArray (". xls", ". xlsx");//the suffix name of the Excel filevarCssext =NewArray (". css");//the suffix name of the CSS filevarJsext =NewArray (". js");//js file suffix name//get file name suffix nameString.prototype.extension =function(){ varext =NULL; varName = This. toLowerCase (); vari = Name.lastindexof (".")); if(I >-1){ varext =name.substring (i); } returnext;}//determine if a value is included in an arrayArray.prototype.contain =function(obj) { for(vari=0; i< This. length; i++){ if( This[i] = = =obj)return true; } return false;};functiontypematch (Type, fielname) {varext =filename.extension (); if(Type.contain (EXT)) {return true; } return false;}
After referencing the above code, if you want to determine whether the file filename is a picture file, just the following:
if (Typematch (imgext, filename)) { alert ("is a picture file"); } Else { alert ("not picture file");}
If you need to determine other file types, you can add an array of suffix polygons to the front, similar to Imgext, and then use the method above to judge.
JavaScript determines file type based on filename