[英]Check for file extension JS
I have some handy code which checks the file extension of a file for me and attaches an image depending on the result. 我有一些方便的代码,可以为我检查文件的文件扩展名并根据结果附加图像。
If the extension doesn't match any of the ones I have listed, I want to attach a generic 'Unknown' image. 如果扩展名与我列出的任何扩展名都不匹配,我想附加一个通用的“未知”图像。 What could I enter into the case to accomplish this?
我可以通过什么案子来实现这一目标?
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;
}
}
Any suggestions? 有什么建议么?
Use default:
使用
default:
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;
}
You wouldn't use a case if nothing matches, you would use default. 如果没有匹配项,则不使用大小写,而是使用default。 It would look like:
它看起来像:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.