Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查文件扩展名JS_Javascript_Jquery - Fatal编程技术网

Javascript 检查文件扩展名JS

Javascript 检查文件扩展名JS,javascript,jquery,Javascript,Jquery,我有一些方便的代码,可以帮我检查文件的扩展名,并根据结果附加一个图像 如果扩展名与我列出的任何扩展名都不匹配,我想附加一个通用的“未知”图像。我可以通过什么途径来完成这一任务 function get_extension(file_name) { return file_name.split('.').pop().toLowerCase(); } function check_file_type(file) { switch(get_e

我有一些方便的代码,可以帮我检查文件的扩展名,并根据结果附加一个图像

如果扩展名与我列出的任何扩展名都不匹配,我想附加一个通用的“未知”图像。我可以通过什么途径来完成这一任务

    function get_extension(file_name) {
        return file_name.split('.').pop().toLowerCase();
    }

    function check_file_type(file) { 
        switch(get_extension(file)) {
            //if .jpg/.gif/.png do something
            case 'jpg': case 'gif': case 'png':
                return '{!URLFOR($Resource.Common, 'images/filetypes/doctype_image.png')}';
                break;
            //if .zip/.rar do something else
            case 'zip': case 'rar':
                return '{!URLFOR($Resource.Common, 'images/filetypes/doctype_zip.png')}';
                break;      
            //if .pdf do something else
            case 'pdf': case 'pptx':
                return '{!URLFOR($Resource.Common, 'images/filetypes/doctype_pdf.png')}';
                break;
            //if .docx do something else
            case 'docx':
                return '{!URLFOR($Resource.Common, 'images/filetypes/doctype_word.png')}';
                break;
            //if unknown do something else
            case 'WHAT DO I DO HERE' <-- ???
                return '{!URLFOR($Resource.Common, 'images/filetypes/doctype_other.png')}';
                break;
        }
    }
函数获取扩展名(文件名){
返回文件_name.split('.').pop().toLowerCase();
}
函数检查文件类型(文件){
开关(获取扩展名(文件)){
//如果.jpg/.gif/.png
案例“jpg”:案例“gif”:案例“png”:
返回“{!URLFOR($Resource.Common,'images/filetypes/doctype_image.png')”;
打破
//如果.zip/.rar执行其他操作
案例'zip':案例'rar':
返回“{!URLFOR($Resource.Common,'images/filetypes/doctype_zip.png')”;
打破
//如果.pdf,请做其他事情
案例“pdf”:案例“pptx”:
返回“{!URLFOR($Resource.Common,'images/filetypes/doctype_pdf.png')”;
打破
//如果.docx,请执行其他操作
案件‘docx’:
返回“{!URLFOR($Resource.Common,'images/filetypes/doctype_word.png')”;
打破
//如果不知道,做点别的

案例“我在这里做什么”使用
默认值:

    switch(get_extension(file)) {
        //if .jpg/.gif/.png do something
        case 'jpg': case 'gif': case 'png':
            return '{!URLFOR($Resource.Common, 'images/filetypes/doctype_image.png')}';
            break;
        //if .zip/.rar do something else
        case 'zip': case 'rar':
            return '{!URLFOR($Resource.Common, 'images/filetypes/doctype_zip.png')}';
            break;      
        //if .pdf do something else
        case 'pdf': case 'pptx':
            return '{!URLFOR($Resource.Common, 'images/filetypes/doctype_pdf.png')}';
            break;
        //if .docx do something else
        case 'docx':
            return '{!URLFOR($Resource.Common, 'images/filetypes/doctype_word.png')}';
            break;
        //if unknown do something else
        default:
            return '{!URLFOR($Resource.Common, 'images/filetypes/doctype_other.png')}';
            break;
    }

如果没有匹配项,则不会使用case,而是使用default。它看起来像:

function get_extension(file_name) {
    return file_name.split('.').pop().toLowerCase();
}

function check_file_type(file) { 
    switch(get_extension(file)) {
        //if .jpg/.gif/.png do something
        case 'jpg': case 'gif': case 'png':
            return '{!URLFOR($Resource.Common, 'images/filetypes/doctype_image.png')}';
            break;
        //if .zip/.rar do something else
        case 'zip': case 'rar':
            return '{!URLFOR($Resource.Common, 'images/filetypes/doctype_zip.png')}';
            break;      
        //if .pdf do something else
        case 'pdf': case 'pptx':
            return '{!URLFOR($Resource.Common, 'images/filetypes/doctype_pdf.png')}';
            break;
        //if .docx do something else
        case 'docx':
            return '{!URLFOR($Resource.Common, 'images/filetypes/doctype_word.png')}';
            break;
        //if unknown do something else
        default:
            return '{!URLFOR($Resource.Common, 'images/filetypes/doctype_other.png')}';
            break;
    }
}

案例陈述中有一个
default
:在
default
案例中不需要使用
break
。引用:与每个case标签相关联的可选break语句确保程序在匹配语句执行后脱离switch,并在switch后面的语句继续执行。如果省略break,程序将在switch语句中的下一个语句继续执行。"从@maxpower9000开始,我倾向于在所有情况下都使用它,只是为了保持一致性。学究式的说,
break
不必要的地方是最后一种情况,这不一定是
default
情况。您可能没有
default
,并且
default
不必是最后一种情况:请确切地查看我的命令中的MDN引号ent声明。注释注释了上面的acual代码,它不是对javascipt本身的注释,在这种情况下,可以在
default
案例中省略
break
。代码仍然很棒,干杯!