jQuery find file extension (from string)

前端 未结 8 1175
半阙折子戏
半阙折子戏 2020-12-13 02:13

I was wondering if it was possible for jQuery to find a file extension based on a returned string?

A filename (string) will be passed to a function (openFile) and I

8条回答
  •  甜味超标
    2020-12-13 02:47

    Yet another way to write it up:

    function getExtension(filename) {
        return filename.split('.').pop().toLowerCase();
    }
    
    function openFile(file) { 
        switch(getExtension(file)) {
            //if .jpg/.gif/.png do something
            case 'jpg': case 'gif': case 'png':
                /* handle */
                break;
            //if .zip/.rar do something else
            case 'zip': case 'rar':
                /* handle */
                break;
    
            //if .pdf do something else
            case 'pdf':
                /* handle */
                break;
        }
    }
    

提交回复
热议问题