JavaScript determines file type based on filename

Source: Internet
Author: User

//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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.