您的位置:首页 > Web前端 > JavaScript

html 通过js获得文件路径(适用于ie)

2014-05-07 16:03 260 查看
网页完整代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title></title>

<link href="../CSS/rightStyle.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">

function getPath() {  
var fileinput = document.getElementById("file");
document.getElementById("filepath").value = getFilePath(fileinput);   
/* alert(document.getElementById("filepath").value); */
}

/* 

推荐IE浏览器,出于安全考虑,其他浏览器限制访问本地文件地址

2014.5.7

 */

function getFilePath(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>

<table width="100%" border="0" cellpadding="0" cellspacing="0"
align="center">
<tr height="30px">
<td width="20px"
style='padding-right: 4px; background-image: url("Images/right_title_1.gif");'>
<img src="Images/icon.gif" style="margin-left: 4px" width="12px"
height="12px" border="0" alt="" /></td>
<td width="100%"
style='background-image: url("Images/right_title_2.gif");'><span
id="RightMenu" style="color: #376EA6; font-size: 12px;">系统配置管理
>数据库恢复</span></td>
<td width="20px"
style='padding-left: 20px; background-image: url("Images/right_title_3.gif");'>
</td>

</tr>
</table>
<form name="form1" id="form1" method="post" action="dataRestore.action">
<div>

<table width="100%" border="0" cellpadding="0" cellspacing="0"
align="center"
style="text-align: center; font-size: 12px; margin-top: 10px"
class="table1">

<tr>
<th colspan="2" height="25px"
style="color: #FFFFFF; background-color: #376ea6; font-size: 13px">
数据库恢复</th>
</tr>
</table>

<fieldset style="margin-top: 10px;">
<legend>
<font style="font-size: 12px;" face="宋体" color="#0066CC">数据库恢复</font>
</legend>

<div style="text-align: center;">
<table width="100%" border="0" cellpadding="0" cellspacing="0"
align="center" style="text-align: center; font-size: 12px;"
class="table1">
<tr>
<td
style="height: 30px; width: 45%; text-align: right; padding-right: 25px; font-size: 13px">
<font color="#DD3A90">* </font>数据库IP地址:</td>
<td align="left"><input type="text" name="ip" id="ip"
style="font-size: 12px; width: 200px;" />
</td>
</tr>
<tr>
<td
style="height: 30px; text-align: right; padding-right: 25px; font-size: 13px">
<font color="#DD3A90">* </font>数据库登陆名:</td>
<td align="left"><input type="text" name="username"
id="username" style="font-size: 12px; width: 200px;" />
</td>
</tr>
<tr>
<td
style="height: 30px; text-align: right; padding-right: 25px; font-size: 13px">
<font color="#DD3A90">* </font>数据库登陆密码:</td>
<td align="left"><input type="text" name="password"
id="password" style="font-size: 12px; width: 200px;" />
</td>
</tr>
<tr>
<td
style="height: 30px; text-align: right; padding-right: 25px; font-size: 13px">
<font color="#DD3A90">* </font>数据库名称:</td>
<td align="left"><input type="text" name="dataname"
id="dataname" style="font-size: 12px; width: 200px;" />
</td>
</tr>
<tr>
<td
style="height: 30px; text-align: right; padding-right: 25px; font-size: 13px">
<font color="#DD3A90">* </font>恢复文件:</td>
<td align="left"><input type="file" name="file"
id="file" style="font-size: 12px; width: 200px;" />

</td>
</tr>
<tr>
<td
style="height: 30px; text-align: right; padd
a0e4
ing-right: 25px; font-size: 13px">
<font color="#DD3A90"></font></td>
<td align="left"><input type="hidden" name="filepath"
id="filepath" style="font-size: 12px; width: 200px;" />

</td>
</tr>
<tr>
<td colspan="3" style="height: 30px;"><input type="submit"
name="submit" value="确定"
onClick="getPath()"
style="margin-left: 60px; color: #376EA6; background-color: #EAEAEA; border-color: #376EA6; border-width: 1px; border-style: solid; font-size: 12px; height: 22px; width: 60px;" />
<input type="reset" name="reset" value="重置"
style="margin-left: 40px; color: #376EA6; background-color: #EAEAEA; border-color: #376EA6; border-width: 1px; border-style: solid; font-size: 12px; height: 22px; width: 60px;" />
</td>
</tr>
</table>
</div>
</fieldset>
</div>
</form>

</body>

</html>

后台可以通过action获得

<input type="hidden" name="filepath"
id="filepath" style="font-size: 12px; width: 200px;" />

的值!

其他浏览器好像现在出于安全性考虑不让获得本地文件地址!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: