如何使用jquery或javascript获取文件的扩展名? [英] How to get a extension of a file using jquery or javascript?

查看:119
本文介绍了如何使用jquery或javascript获取文件的扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查上传文件的类型。如果名称像 example.txt ,我只想获得 .txt 部分。我如何使用Jquery或javascript实现它。

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.

任何建议或链接都​​会很感激!!!

Any Suggestions or links would be appreciative!!!

推荐答案

一个简单的解决方案是 .split () .pop() 获取数组中的最后一个字符串,如下所示:

A simple solution is .split() and .pop() to get the last string in the array, like this:

var ext = fileName.split('.').pop();

这样只需txt没有,只需在需要时附加。这也适用于: My.File.name.has.an extension.txt 。如果没有有扩展名,它将返回文件名,因此您可能需要检查这个...或者采用完全不同的方向并通过正则表达式验证一组或已知的扩展名。

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't have 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.

这篇关于如何使用jquery或javascript获取文件的扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆