因为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. 学习笔记 之--AJAX核心对象 XMLHttpRequest

    实现AJAX功能,核心就是XMLHttpRequest,而且现在大多数浏览器都支持这个核心组件对象. 实例:实现无刷新登陆验证 1.前台代码 <html xmlns="http://w ...

  2. nginx重定向规则详细介绍

    为何要使用301重定向 在网站建设中需要网页重定向的情况很多:如网页目录结构变动,网页重命名.网页的扩展名改变.网站域名改变等.如果不做重定向,用户的收藏和搜索引擎数据库中的旧地址只能让访客得到一个4 ...

  3. 用Set中元素做条件查询

    一个老师教许多学生,一个学生被许多老师教,一个学生有好多书,同一种书被许多同学拥有.查询教拥有书"a"的学生的老师:   class teacher{   String id;   ...

  4. 一些不太常用的Linux命令

    ACCTCOM 查看所有用户执行过的进程 acctcom | tail - 查看指定用户执行过的进程/命令 acctcom -u <username> | tail - 使用一个正则表达式 ...

  5. C程序设计语言练习题1-19

    练习1-19 编写函数reverse(s),将字符串s中的字符顺序颠倒过来.使用该函数编写一个程序,每次颠倒一个输入行中的字符顺序.代码如下: #include <stdio.h> // ...

  6. linux“批处理”脚本

    依次执行A B C3条语句 最简单的 写如下代码 #!/bin/sh A B C 保存为test.sh然后添加执行权限chmod +x test.sh然后执行该脚本./test.sh

  7. Linux 在一个命令行上执行多个命令(转载)

    对于单个命令执行我想大多数人都是明了的,也就是在一个命令行上执行一条命令.那对于在一行上执行多个命令怎么办呢,其实也很简单,只需在各命令之间加上特殊命令符号,我们常规使用到的有3个特殊命令符号. 1. ...

  8. 【学习笔记】【Foundation】数组

    数组:可重复,有序 不可变数组 创建数组 //array开头的方法是类方法,init开头的是实例方法 //NSArray* arr=[[NSArray alloc]initWithObjects:@& ...

  9. Swift—扩展声明-备

    声明扩展的语法格式如下: extension 类型名 { //添加新功能 } 声明扩展的关键字是extension,“类型名”是Swift中已有的类型,包括类.结构体和枚举,但是我们仍然可以扩展整型. ...

  10. i++ 与 ++i 的从字节码层面看二者的区别

    /** * javap命令可以对class反汇编得到其字节码文件(此命令并不是jdk8开始的,只不过jdk8中对工具进行加强,增加了一些参数,可通过 javap -help了解) * * 注意: * ...