struts2 JS获取上传文件的绝对路径,兼容IE和FF
因为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的更多相关文章
- JS获取上传文件的绝对路径,兼容IE和FF
<input type="file" id="fileBrowser" name="fileBrowser" size="5 ...
- js获取上传文件的绝对路径
在html中 <input type="file" id="importFile" /> <input type="bu ...
- Atitit.js获取上传文件全路径
Atitit.js获取上传文件全路径 1. 默认的value只能获取文件名..安全原因.. 1 2. Firefox浏览器的读取 1 3. Html5 的file api 2 4. 解决方法::使用a ...
- js获取上传文件内容(未完待续)
js 获取上传文件的字节数及内容 <div> 上传文件 : <input type="file" name = "file" id = &qu ...
- js获取上传文件内容
js 获取上传文件的字节数及内容 <div> 上传文件 : <input type="file" name = "file" id = &qu ...
- js 获取上传文件的字节数及内容
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JS 获取上传文件的内容
<div> 上传文件 : <input type="file" name = "file" id = "fileId" / ...
- JS获取上传文件的名称、格式、大小
<input id="File1" type="file" onchange="checkFile(this)" /> 方式一) ...
- js获取上传文件个数 以及名称
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
随机推荐
- SSM框架搭建java.lang.ClassNotFoundException: org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
在搭建 spring springMVC Mybatis 时候出错 将org.springframework.http.converter.json.MappingJacksonHttpMessage ...
- ionic框架,快速开发webAPP神器。
官网地址 http://www.ionicframework.com/ 这个国外框架已经很火了.会使用插件的话更好,例如支付宝支付插件,调用摄像头拍照,二维码扫描,通讯录,文件上传,推送信息等等. 最 ...
- 转:AM335X 启动流程
链接: http://blog.csdn.net/hudaweikevin/article/details/10376585 作者:David_Hu 启动顺序(针对TI OMA3 EVM) linu ...
- IE 将“通过域访问数据源”设置为启用(注册表)
XP HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1\1406 Vista+ HKCU\Softwar ...
- Yii数据库
http://www.yiiframework.com/doc/guide/1.1/zh_cn/database.ar http://zhengdl126.iteye.com/blog/1460779 ...
- ubuntu下编译时遇到的错误及解决方式
1.错误展示: hangma@ubuntu:~/test/test/protest/stack_test$ gcc -c my_stack.c -o my_stack.o In file inclu ...
- Android豆瓣图书查询Demo
原文出自:方杰| http://fangjie.info/?p=26 转载请注明出处 首先先看一下Demo预览效果吧,主要也就是两个Activity.涉及到的技术有zxing开源项目的使用,网络协议豆 ...
- HDU_2013——蟠桃记,反向推理
Problem Description 喜欢西游记的同学肯定都知道悟空偷吃蟠桃的故事,你们一定都觉得这猴子太闹腾了,其实你们是有所不知:悟空是在研究一个数学问题!什么问题?他研究的问题是蟠桃一共有多少 ...
- C++豆知识索引
C++Memset误区 C++ 哈希表 C++类型转换[转] C++ sizeof C++ 路径中\\与/
- jquery $.each()用法
今天看到一个新的each玩法即each作为jquery的函数(平时用的大概都是用的each方法)使用: $.each([ 52, 97 ], function( index, value ) { al ...