How can I get file extensions with JavaScript?

后端 未结 30 1815
终归单人心
终归单人心 2020-11-22 09:37

See code:

var file1 = \"50.xsl\";
var file2 = \"30.doc\";
getFileExtension(file1); //returns xsl
getFileExtension(file2); //returns doc

function getFileExt         


        
30条回答
  •  孤街浪徒
    2020-11-22 10:03

    Wallacer's answer is nice, but one more checking is needed.

    If file has no extension, it will use filename as extension which is not good.

    Try this one:

    return ( filename.indexOf('.') > 0 ) ? filename.split('.').pop().toLowerCase() : 'undefined';
    

提交回复
热议问题