jQuery find file extension (from string)

前端 未结 8 1139
半阙折子戏
半阙折子戏 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:41
    var fileName = 'file.txt';
    
    // Getting Extension
    
    var ext = fileName.split('.')[1];
    
    // OR
    
    var ext = fileName.split('.').pop();
    
    0 讨论(0)
  • 2020-12-13 02:46

    Another way (which avoids extended switch-case statements) is to define arrays of file extensions for similar processing and use a function to check the extension result against an array (with comments):

    // Define valid file extension arrays (according to your needs)
    var _docExts = ["pdf", "doc", "docx", "odt"];
    var _imgExts = ["jpg", "jpeg", "png", "gif", "ico"];
    // Checks whether an extension is included in the array
    function isExtension(ext, extnArray) {
        var result = false;
        var i;
        if (ext) {
            ext = ext.toLowerCase();
            for (i = 0; i < extnArray.length; i++) {
                if (extnArray[i].toLowerCase() === ext) {
                    result = true;
                    break;
                }
            }
        }
        return result;
    }
    // Test file name and extension
    var testFileName = "example-filename.jpeg";
    // Get the extension from the filename
    var extn = testFileName.split('.').pop();
    // boolean check if extensions are in parameter array
    var isDoc = isExtension(extn, _docExts);
    var isImg = isExtension(extn, _imgExts);
    console.log("==> isDoc: " + isDoc + " => isImg: " + isImg);
    // Process according to result: if(isDoc) { // .. etc }
    
    0 讨论(0)
  • 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;
        }
    }
    
    0 讨论(0)
  • 2020-12-13 02:51

    Since the extension will always be the string after a period in a complete/partial file name, just use the built in split function in js, and test the resultant extension for what you want it to do. If split returns only one piece / no pieces, it does not contain an extension.

    0 讨论(0)
  • 2020-12-13 02:52

    To get the file extension, I would do this:

    var ext = file.split('.').pop();
    
    0 讨论(0)
  • 2020-12-13 03:00

    Try this:

    var extension = fileString.substring(fileString.lastIndexOf('.') + 1);
    
    0 讨论(0)
提交回复
热议问题