编写监听器以及相关的类 字串6
不用自己写了,源码可以从 字串6
http://www.telio.be/blog/2006/01/06/ajax-upload-progress-monitor-for-commons-fileupload-example里面下载,
字串8
下载地址 字串2
http://www.telio.be/blog/wp-content/uploads/2006/01/ajax-upload-1.0.war 字串3
下载完之后,你需要解包,在命令行中:
jar xvf ajax-upload-1.0.war 字串6
源码就在\WEB-INF\src下面 字串7
将要用到的文件有resources文件夹下,\WEB-INF\下面的dwr.xml和lib下面的jar文件 字串4
上传页面 字串8
在上面解开的包里面有index.jsp这个是上传页面,upload.jsp负责上传,resources包里面包含了dwr与服务器通讯的javascript脚本,你需要把index.jsp的form中的action换成你的struts action即可,然后把form中的file名改成你actionform中的file属性名,把页面中剩余的file去掉
字串1
index.jsp上传页面示例代码: 字串8
<%@ page contentType="text/html; charset=gb2312" language="java" 字串8
import="java.util.*" errorPage=""%> 字串3
<% 字串5
String path=request.getContextPath();
%> 字串6
<html locale="true">
<head> 字串8
<SCRIPT language=javascript>
字串6
function check_file() {
var strFileName=document.forms(0).file;
if (strFileName.value==""){ 字串7
alert("请选择要上传的文件"); 字串2
return false;
}
字串8
startProgress();
字串7
}
function loadmessage(){
字串5
<% 字串4
if (request.getAttribute("message") != null) { 字串9
%> 字串1
window.alert("<%=request.getAttribute("message")%>"); 字串3
window.returnValue="yes";
<%}%>
}
字串3
</SCRIPT> 字串4
<title>上传</title> 字串9
<script src='<%=path%>/resources/js/upload.js'> </script>
<script src='<%=path%>/dwr/interface/UploadMonitor.js'> </script>
<script src='<%=path%>/dwr/engine.js'> </script> 字串5
<script src='<%=path%>/dwr/util.js'> </script> 字串1
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" onLoad="loadmessage()"> 字串3
<form name="form" method="post" action="<%=path%>/upload.do?method=upload" enctype="multipart/form-data" onSubmit="return check_file()"> 字串7
<table width="60%" border="0" cellspacing="1"> 字串4
<tr background-color=" #E7F5FE">
<td width="30%" align="right">从文件导入:</td> 字串2
<td width="51%"><input type="file" name="file" class="input"></td> 字串1
<td width="27%"><input type="submit" name="files" class="button_4"
字串7
style="border-style:None;width:71px;" value="确定" id="uploadbutton"> 字串2
</td> 字串4
</tr>
字串7
<tr align="left" background-color=" #E7F5FE">
字串4
<td colspan="3"> 字串6
<div id="progressBar" style="display: none;">
字串3
字串6

