因为file控件上传失败后会自动清空,所以使用文本框来保存上传路径,而且在不同的浏览器下,控件的样式也需要兼容。下面是自己用到的实例

// 初始化判断浏览器的版本,根据版本的不同使用不同的样式
function getExplorer() {
//IE
if (navigator.userAgent.indexOf("MSIE")>=0)
{
document.getElementById("1").style.display = "block";
document.getElementById("2").style.display = "none";
document.getElementById("FileUpload").className = "file";
document.getElementById("box").className = "file-box";
}
//Firefox
else if (navigator.userAgent.indexOf("Firefox")>=0)
{
document.getElementById("1").style.display = "none";
document.getElementById("2").style.display = "block";
document.forms[0].elements["propertyName"].style.width="625px";
}
//Chrome
else if(navigator.userAgent.indexOf("Chrome") >=0)
{
document.getElementById("1").style.display = "block";
document.getElementById("2").style.display = "none";
document.forms[0].elements["propertyName"].style.width="495px";
document.getElementById("ieBtn").style.display = "none";
document.getElementById("FileUpload").style.width = "93px";
}
//Safari
else if(navigator.userAgent.indexOf("Safari") >=0 )
{
document.getElementById("1").style.display = "block";
document.getElementById("2").style.display = "none";
document.forms[0].elements["propertyName"].style.width="585px";
document.getElementById("ieBtn").style.display = "none";
document.getElementById("FileUpload").style.width = "90px";
}
}

样式的代码

.file-box{ position:relative;width:55px}
.btn{ width:55px;}
.file{ position:absolute;top:; right:0px; height:24px; filter:alpha(opacity:0);opacity:;width:0px }

html的代码

由于使用的是struts2

控件的name是formInfo.propertyName

修改的场合时,控件的value是前面的action通过request.setAttribute("Form", pForm)设置的,jsp接收时必须按照下面那么写

<table align="center">
<tr>
<td ><s:textfield name="formInfo.propertyName" value="%{#request.Form.propertyName}" size="512" maxlength="512" style="width:620px;" readonly="true" /></td>
<td id="1">
<div id="box">
<input type="button" value="参照" class="btn" id="ieBtn" />
<input type="file"name="filepropertyName" id="FileUpload" size="1" onchange="Change()" />
</div>
</td>
<td id="2"><input type="button" name="upload" class="btn" id="upload" value="参照" onclick="selectPDF()"/></td>
<td ><input type="button" value="クリア" class="btn" onclick="delPdf()"/></td>
</tr>
</table>

单击按钮把路径显示到文本框中的js

// 当file的值改变时 把路径赋值给文本框
function Change() {
//IE
if (navigator.userAgent.indexOf("MSIE")!=-1||navigator.userAgent.indexOf("Chrome") !=-1||navigator.userAgent.indexOf("Safari") !=-1)
{
var localpath = document.getElementById("FileUpload").value;
document.forms[0].elements["propertyName"].value = localpath;
}
//FF
else if (navigator.userAgent.indexOf("Firefox")!=-1)
{
readFileFirefox()
}
} //FF
function readFileFirefox() {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
}
catch (e) {
return;
} var fileName=document.getElementById("FileUpload").value;
var file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
try {
file.initWithPath( fileName.replace(/\//g, "\\\\") );
}
catch(e) {
if (e.result!=Components.results.NS_ERROR_FILE_UNRECOGNIZED_PATH) throw e;
return;
} if ( file.exists() == false ) {
alert("File '" + fileName + "' not found.");
return;
}
document.forms[0].elements["propertyName"].value =file.path;
} // 火狐浏览器下 单击按钮打开file控件 弹出文件选中对话框
function selectPDF()
{
document.forms[0].FileUpload.click();
}

struts2 JS获取上传文件的绝对路径,兼容IE和FF的更多相关文章

  1. JS获取上传文件的绝对路径,兼容IE和FF

    <input type="file" id="fileBrowser" name="fileBrowser" size="5 ...

  2. js获取上传文件的绝对路径

    在html中    <input type="file" id="importFile" />    <input type="bu ...

  3. Atitit.js获取上传文件全路径

    Atitit.js获取上传文件全路径 1. 默认的value只能获取文件名..安全原因.. 1 2. Firefox浏览器的读取 1 3. Html5 的file api 2 4. 解决方法::使用a ...

  4. js获取上传文件内容(未完待续)

    js 获取上传文件的字节数及内容 <div> 上传文件 : <input type="file" name = "file" id = &qu ...

  5. js获取上传文件内容

    js 获取上传文件的字节数及内容 <div> 上传文件 : <input type="file" name = "file" id = &qu ...

  6. js 获取上传文件的字节数及内容

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. JS 获取上传文件的内容

    <div> 上传文件 : <input type="file" name = "file" id = "fileId" / ...

  8. JS获取上传文件的名称、格式、大小

    <input id="File1" type="file" onchange="checkFile(this)" /> 方式一) ...

  9. js获取上传文件个数 以及名称

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

随机推荐

  1. linux笔记2.20

    用户相关:  /etc/passwd  用户信息 /etc/shadow  密码信息 /etc/group  组信息 添加用户:   useradd   -u -g 修改用户: usermod   - ...

  2. Android webview 应用

    现在在android把一个网站打包成apk 是很简单的事,不懂一点代码的都可以去百度生成一个apk,app.当然样式就不是你所能控制的. 今天我就介绍一下 android 的 webview 在And ...

  3. 统计建模与R软件习题二答案

    # 习题2 # 2.1 x=c(1,2,3) y=c(4,5,6) e=c(rep(1,3)) z=2*x+y+e;z x%*%y # 若x,y如答案那样定义为矩阵,则不能用%*%,因为,维数不对应, ...

  4. 黑马程序员——C语言开门片内存分析

    iOS培训,iOS学习---------型技术博客.期待与您交流!------------ 一.各种进制的总结 1.二进制 (1) 在c语言中二进制以0b开头,输出二进制格式没有固定的格式,自定义输出 ...

  5. JS操作css的float属性的特殊写法

    使用js操作css属性的写法是有一定的规律的: 1.对于没有中划线的css属性一般直接使用style.属性名即可. 如:obj.style.margin,obj.style.width,obj.sty ...

  6. Entity Framework with MySQL 学习笔记一(验证标签)

    直接上代码 [Table("single_table")] public class SingleTable { [Key] public Int32 id { get; set; ...

  7. QT中异形窗口的绘制(winEvent处理WM_NCHITTEST消息)

    这里讨论的只是Windows平台上的实现. 在QT中绘制异形窗口,只要设定 windowFlag 为 CustomizeWindowHint,再结合setMask()就可以做出各种奇形怪状的窗口.相对 ...

  8. eclipse中格式化代码快捷键Ctrl+Shift+F失效的解决办法

    很长一段时间我的eclipse都有个毛病,就是当我要格式化代码的时候,右键-source-format能够起效,但ctrl+shift+f不好使了. 可以基本断定是快捷键与别的软件冲突了,但一直也没时 ...

  9. VC++6.0注释快捷键设置

    在Qt Creator,eclipse等编辑器中,都默认有注释代码的快捷键:Ctrl + /. 注释快捷键在程序编程当中的作用相当明显,提高了编程效率.我在网上找到了一个在VC++6.0工具中添加注释 ...

  10. HDU_2136——最大质因数,素数筛选法

    Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...