![](/img/trans.png)
[英]Get Selected Filename in dynamic input file fields using jQuery/JavaScript
[英]Alert input “file” filename using jQuery with “split”
我正在尝试从上传输入中提醒文件名。
这是我的小提琴
它可以工作,但是有类似的“ C:Fakepath ...”。 我只想要文件名,没有伪路径。 我尝试使用split
函数,但不确定为什么它不起作用。
范例html:
<html>
<head>
<title>Test Page</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<input type="file" id="tester" />
</body>
</html>
示例脚本:
$(function() {
var bogus;
var triple;
$('#tester').change(function() {
triple = $('#tester').val();
bogus = triple.split(/[\s/]+/);
alert(bogus[bogus.length - 1]);
});
});
有什么想法吗?
谢谢!
如果您的Web浏览器支持File API (当前是不是IE的任何Web浏览器),则可以从输入的FileList对象获取文件名。
$(function() {
$('#tester').change(function() {
var files = this.files;
if (files && files.length) {
alert(files[0].name); //use the file api
}
else {
alert($(this).val().replace("C:\\fakepath\\", "")); //this is for IE
}
});
});
示例: http : //jsfiddle.net/NTICompass/xwdct/6/
文件: https : //developer.mozilla.org/en-US/docs/Using_files_from_web_applications
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.