java web 开发 遇到的js问题

惟Ai你sha 2014-09-24 02:56:35
IE8上传文件时javascript取文件的真实路径的问题("C:\fakepath\xxxxx )

<title>My JSP 'fileupload.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function click1(){
var file = document.getElementById('file');
alert(getPath(file));
}
function getPath(obj) {
if(obj){
if (window.navigator.userAgent.indexOf("MSIE")>=1){
obj.select();
return document.selection.createRange().text;
}else if(window.navigator.userAgent.indexOf("Firefox")>=1)
{
if(obj.files){
return obj.files.item(0).getAsDataURL();
}
return obj.value;
}
return obj.value;
}

}
</script>
</head>

<body>
文件
<input type="file" id="file" name="file" />
<input type="button" onClick="click1();" value="submit">
</body>
</html>
很简单的一个js代码 放在本地访问修改后缀名为html js可以运行 没问题的

可是我放在我服务器中访问不运行 点击按钮没反应 不管是html还是jsp都没反应映


于是我alert(document.getElementById('file').value)可以得到对象
但是alert(document.getElementById('file').select())是未定义

在网上查了查说document.getElementById('file')没有select()属性 可是为什么本地可以不报错呢




求解 很郁闷

更多0
...全文
128 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
惟Ai你sha 2014-09-24
  • 打赏
  • 举报
回复
引用 5 楼 gagewang1 的回复:
放弃吧,我以前也就试过,这样的js只能针对部分的浏览器有效,兼容性是个大问题。。。。
唉 没办法
中华雪碧 2014-09-24
  • 打赏
  • 举报
回复
放弃吧,我以前也就试过,这样的js只能针对部分的浏览器有效,兼容性是个大问题。。。。
惟Ai你sha 2014-09-24
  • 打赏
  • 举报
回复
引用 3 楼 gagewang1 的回复:
为了安全,现在的浏览器,js不直接获取文件的路径。本地的可以,是因为不存在安全的问题。么么哒
确实是我上面的代码就是解决上面的问题 在解决问题的同时 产生的问题 现在问题是js在服务器不往下走 t(document.getElementById('file').select())是未定义
中华雪碧 2014-09-24
  • 打赏
  • 举报
回复
为了安全,现在的浏览器,js不能直接获取文件的路径。本地的可以,是因为不存在安全的问题。么么哒
惟Ai你sha 2014-09-24
  • 打赏
  • 举报
回复
引用 1 楼 tan3739 的回复:
select : 突出显示表单元素的输入区域。 select Method Highlights the input area of a form element. No return value. Use this method with the focus method to highlight a field and position the cursor for a user response.
你好 那个为什么在本地测试时没问题的呢 还有如果那样的话我该怎样操作呢 谢谢
sugarTan 2014-09-24
  • 打赏
  • 举报
回复
select : 突出显示表单元素的输入区域。 select Method Highlights the input area of a form element. No return value. Use this method with the focus method to highlight a field and position the cursor for a user response.

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧