日期:2014-05-16  浏览次数:20408 次

jquery获取file控件文件名,为什么要是C:\fakepath\***.jpg,我就想获取文件名不要后缀怎么弄
<script type="text/javascript">
$(function(){
$("#file").change(function(){
var filePath=$(this).val();

$("#txt1").val(filePath);
});
});
</script>
<title>无标题文档</title>
</head>

<body>
<input type="file" id="file" />
<input type="text" id="txt1" />
</body>
怎么获取文件名啊,不要C:\fakepath\这个东西
------解决方案--------------------
自己切割下字符串  或者html5的file对象
------解决方案--------------------
引用:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml1.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
<script type="text/javascript">
$(function(){
$("#file").change(function(){
var filePath=$(this).val();
$("#txt1").val(filePath);
});
});
</script>
</head>
<body>
<input type="file" id="file" />
<input type="text" id="txt1" />

</body>
</html>


这写法也是不行的,因为浏览器不允许读取本地文件。所以会抛出fakepath

你的是载入图片,那么webkit之类的现代浏览器可以用img.onload实现。
但低版本的IE用不了这玩意,得用document.selection.createRange().text
------解决方案--------------------
纯JS分割字符串也是可以的啊