因为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. javascript解析从服务器返回的json格式数据

    在javascript中我们可以将服务器返回的json格式数据转换成json格式进行使用,如下: 1.服务器返回的json格式数据: 通过response.responseText获得: " ...

  2. ext中处理Combobox组件点击触发后台事件的问题

    ext的Combobox组件在绑定数据的时候需要一个Store来绑定数据,在store里面我们可以设置autoLoad属性,这个属性表示Store可以自动的到后台获取数据,ext实质上就是封装好的ja ...

  3. [译] 什么是移动友好的 - Mobile-friendliness

    什么是移动友好的?   移动友好到底意味着什么取决于您与谁讨论它. 我们不妨这样理解: 移动友好的设计以 表现力, 内容和性能 三方面为目标来改善用户体验.   表现力 - Presentation ...

  4. php urlencode()函数URL编码转换实例解析

    URLEncode:是对网页url所包含中文字符的一种编码转化方式,URLEncode有两种常见方式,一种是基于GB2312的 Encode(Baidu.Yisou等搜索引擎使用),另一种是基于UTF ...

  5. fabric自动化部署django

    使用fabric部署django应用 使用fabric部署django应用 本文是我的网站易读中文网自动化部署的脚本实现,以下代码在ubuntu和debian中测试通过 由于网站使用的是python技 ...

  6. HTTP_X_FORWARDED_FOR 和 REMOTE_ADDR的使用 php

    参考来源:http://qq398705749.iteye.com/blog/963818 php中HTTP_X_FORWARDED_FOR 和 REMOTE_ADDR的使用 1.REMOTE_ADD ...

  7. iOS NSDictionary、NSData、JSON等 数据类型相互转换

    1.NSDictionary类型转换为NSData类型: NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: @&qu ...

  8. c++中构造函数 、析构函数的作用域详解

    我们知道,在c++中,析构函数是在函数作用域尾部执行析构函数,从而释放对象,但是有一种情况下,析构函数作用域发生变化,请看下面的例子,定义了一个Stock类,Stock类存放在stock.h中,主调用 ...

  9. 转:MFC创建多线程实例

    作者:http://blog.csdn.net/wangningyu/article/details/4404134 平时在MFC里使用多线程时其实是很方面的,因为微软提供了一个API让我们很方面的去 ...

  10. Android中的webview的进度条

    <application android:icon="@drawable/hunqin" android:label="@string/app_name" ...