如何使用 jquery 或 javascript 获取文件的扩展名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4164894/
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
How to get a extension of a file using jquery or javascript?
提问by IamIronMAN
i want to check the type of uploaded file. If name like example.txt
, i want to get the .txt
part only. How can i achieve it using Jquery or javascript.
我想检查上传文件的类型。如果名字像example.txt
,我只想得到那.txt
部分。我如何使用 Jquery 或 javascript 实现它。
Any Suggestions or links would be appreciative!!!
任何建议或链接将不胜感激!!!
回答by Nick Craver
A simple solution is .split()
and .pop()
to get the last string in the array, like this:
一个简单的办法是.split()
和.pop()
获得最后一个字符串数组中,就像这样:
var ext = fileName.split('.').pop();
This will get you just "txt"
without the .
, just append if needed. This also works on say: My.File.name.has.an extension.txt
as well. If it doesn'thave an extension it'll return the file name, so you may want to check for this...or go a completely different direction and validate against a set or known extensions via regex.
这将使您"txt"
无需.
,只需在需要时附加。这也适用于 say: My.File.name.has.an extension.txt
。如果它没有扩展名,它会返回文件名,所以你可能想要检查这个……或者去一个完全不同的方向并通过正则表达式验证一组或已知的扩展名。
回答by Frédéric Hamidi
Use lastIndexOf()and substr():
function getFileExtension(name)
{
int found = name.lastIndexOf('.') + 1;
return (found > 0 ? name.substr(found) : "");
}
Note that this implementation returns an empty string if the filename doesn't contain any period character (i.e. has no extension). Implementations based on split()sometimes return the full filename in that case.
请注意,如果文件名不包含任何句点字符(即没有扩展名),则此实现将返回一个空字符串。在这种情况下,基于split() 的实现有时会返回完整的文件名。
回答by Evan Mulawski
If you need to validate multiple extensions:
如果您需要验证多个扩展:
var filename = "picture.jpg";
var valid_extensions = /(\.jpg|\.jpeg|\.gif)$/i;
if(valid_extensions.test(filename))
{
alert('OK');
}
else
{
alert('Invalid File');
}
This eliminates the need to parse the string if you want to check the extension, for example, before uploading a file.
如果您想检查扩展名,例如在上传文件之前,则无需解析字符串。
回答by Andrew Collins
if((document.form1.upload.value.lastIndexOf(".jpg")==-1) {
alert("Please upload only .jpg extention file");
return false;
}
lastIndexOf will return the index of the last occurrence of the specified search argument. If not found, -1 will return
lastIndexOf 将返回指定搜索参数最后一次出现的索引。如果没有找到,-1 将返回
回答by tbleckert
var text = 'example.txt',
ext = text.split('.')[1];
回答by Shyju
Split the entire file name with . as the delimitter.The split will store the splitted items in an array.Take the last element of the array.
用 . 分割整个文件名。作为分隔符。拆分会将拆分的项目存储在数组中。取数组的最后一个元素。
回答by Minimul
Frédéric's answer worked well for me, however, the function keep erroring out in Chrome console with "Unexpected identifier". The "int" was issue and this modification worked.
Frédéric 的回答对我来说效果很好,但是,该功能在 Chrome 控制台中不断出错,并显示“意外标识符”。“int”是问题,此修改有效。
function getFileExtension(name){
var found = name.lastIndexOf('.') + 1;
return (parseInt(found) > 0 ? name.substr(found) : "");
}