从Internet Explorer中的文件输入中只获取文件名 [英] Get only file name from file input on internet explorer

查看:152
本文介绍了从Internet Explorer中的文件输入中只获取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 < input type =fileid我只需要从HTML输入文件返回文件名。 =whatever/> 

用于获取文件名称的JavaScript代码是:

  document.getElementById(whatever)。value; 

在firefox中,它只给出我想要的文件名,但在IE中我得到满路径。



我认为字符串操作是获取名称的唯一方法。



什么是最简单/在JavaScript中只获取名称(扩展名)的最简单方法是什么?感谢。

code> var path = document.getElementById(whatever).value;
var fileName = path.match(/ [^ \ / \\] + $ /);
console.log(fileName);


I need to return only the file name from an HTML input file.

<input type="file" id="whatever" />

The JavaScript code im using to get the name of the file is:

document.getElementById("whatever").value;

In firefox it gives only the file name which is what I want, but in IE I get the full path.

I think that string manipulation is the only way to get the name.

What would be the easiest/shortest way to get only the name (extension too) in JavaScript? Thanks.

解决方案

You can try this

var path = document.getElementById("whatever").value;
var fileName = path.match(/[^\/\\]+$/);
console.log(fileName);

这篇关于从Internet Explorer中的文件输入中只获取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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