jQuery 查找文件扩展名(来自字符串)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3042312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery find file extension (from string)
提问by SoulieBaby
I was wondering if it was possible for jQuery to find a file extension based on a returned string?
我想知道 jQuery 是否可以根据返回的字符串找到文件扩展名?
A filename (string) will be passed to a function (openFile) and I wanted that function to do different things based on what file has been passed through, it could be image files or pdf files.
文件名(字符串)将传递给函数(openFile),我希望该函数根据传递的文件执行不同的操作,可以是图像文件或 pdf 文件。
function openFile(file) {
//if .jpg/.gif/.png do something
//if .zip/.rar do something else
//if .pdf do something else
};
I've been looking for something that will find the file's extension but I can't seem to find anything.
我一直在寻找可以找到文件扩展名的东西,但我似乎找不到任何东西。
回答by user113716
How about something like this.
这样的事情怎么样。
Test the live example:http://jsfiddle.net/6hBZU/1/
测试现场示例:http : //jsfiddle.net/6hBZU/1/
It assumes that the string will always end with the extension:
它假设字符串总是以扩展名结尾:
function openFile(file) {
var extension = file.substr( (file.lastIndexOf('.') +1) );
switch(extension) {
case 'jpg':
case 'png':
case 'gif':
alert('was jpg png gif'); // There's was a typo in the example where
break; // the alert ended with pdf instead of gif.
case 'zip':
case 'rar':
alert('was zip rar');
break;
case 'pdf':
alert('was pdf');
break;
default:
alert('who knows');
}
};
openFile("somestring.png");
EDIT: I mistakenly deleted part of the string in openFile("somestring.png");
. Corrected. Had it in the Live Example, though.
编辑:我错误地删除了openFile("somestring.png");
. 更正。不过,在 Live Example 中有它。
回答by Doug Neiner
To get the file extension, I would do this:
要获得文件扩展名,我会这样做:
var ext = file.split('.').pop();
回答by artlung
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;
}
}
回答by rahul
You can use a combination of substringand lastIndexOf
您可以使用substring和lastIndexOf的组合
Sample
样本
var fileName = "test.jpg";
var fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1);
回答by Amr SubZero
var fileName = 'file.txt';
// Getting Extension
var ext = fileName.split('.')[1];
// OR
var ext = fileName.split('.').pop();
回答by futureelite7
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.
由于扩展名始终是完整/部分文件名中句点后的字符串,因此只需使用 js 中的内置 split 函数,并测试生成的扩展名是否符合您的要求。如果 split 只返回一件/没有件,则它不包含扩展名。
回答by Jacob Relkin
Try this:
尝试这个:
var extension = fileString.substring(fileString.lastIndexOf('.') + 1);
回答by RickL
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):
另一种方法(避免扩展 switch-case 语句)是为类似处理定义文件扩展名数组,并使用函数根据数组检查扩展结果(带注释):
// 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 }