例子,javascript 上传多个附件(struts)代码。
<%@ page contentType="text/html;charset=GBK" language="java" %>
<%@ include file="../common/taglib.jsp"%>
<%@ include file="../common/common.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>js多文件上传-www.jb200.com</title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<link href="../resource/css/master.css" rel="stylesheet" type="text/css" />
<link href="../resource/css/software.css" rel="stylesheet" type="text/css" />
<link href="../resource/css/pagination.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../resource/js/global.js"></script>
<script type="text/javascript" src="../resource/js/common.js"></script>
<script type="text/javascript" src="../resource/js/calendar/calendar.js"></script>
<script type="text/javascript" src="../resource/js/validator.js"></script>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/schemaDropDownList.js'></script>
<!--.smallInput{border:1px solid #333333;color:#666666;background:#eeeeee;font:normal 12px Tahoma;height:22px;margin-right:5px;}-->
<style type="text/css">
.smallInput0{border:1px solid #333333;color:#666666;background:white;font:normal 12px Tahoma;height:22px;margin-right:5px; width:300px;}
.smallInput1{border:1px solid #333333;color:#666666;background:white;font:normal 12px Tahoma;height:22px;margin-right:5px;width:50px;}
</style>
<script type="text/javascript">
window.onload = function(){
//alert("test");
if(window.attachEvent){//IE
var obj1 = document.getElementById("nodeId");
var id = obj1.options[obj1.selectedIndex].value;
//alert(id);
selectChange(id);
}else if(window.addEventListener){//firefox
}
}
function selectChange(nodeID){
//alert(nodeID);
schemaDropDownList.getBagInforList(nodeID+"",setDropDownList);
}
function setDropDownList(str){
var _str = eval(str);
var drop = document.getElementById("processId");
drop.length = 0;
var obj ;
for(var i=0;i<_str.length;i++){
obj =_str[i];
drop.options.add(new Option(obj.name+"",obj.id+""));
}
}
function savaSubmit(){
var file = document.getElementById("addFileForm0");
if(file.value==null && file.value == ""){
alert("上传的附件不能为空!");
return ;
}
/*
var table = $("addFileForm");
var files = table.getElementsByTagName("input");
var length = files.length;
for(var i=0;i<length;i++){
var file = files[i];
if(file.getAttribute("type") == "file" ||
file.getAttribute("type")=="FILE"){
alert(file.value);
}
}
*/
//return false;
document.forms[0].submit();
}
/* 新增上传表单start*/
var rowId = 10;
function $(s){
return document.getElementById(s);
}
function addFile(tabid,varName)
{
var tab,row,td,fName,fId,tdStr;
tab = $(tabid);
//fName = varName+;
fId = varName+rowId;
row = tab.insertRow();
row.id = "fileRow"+rowId;
td = row.insertCell();
tdStr="<input type="file" name=""+fId+"" id=""+fId+"" class=smallInput0>";
tdStr+="<input type="button" name="button"+rowId+"" onclick="delTr('fileRow"+rowId+"');" value="删除" class=smallInput1>";
td.innerHTML = tdStr;
rowId = rowId+1;
}
function delTr(row_id)
{
var row;
row = $(row_id);
row.removeNode(row_id);
}
/* 新增上传表单 end*/
</script>
</head>
<body>
<html:form action="/saveSchemaInfor" method="post" enctype="multipart/form-data">
<div id="list">
<div id="list-inner">
<div id="listToolBar">
<ul>
<li><a href="javascript:historyBack();"><img mouseover="true" src="../resource/images/mpm/button/toolbar_back.gif" alt="" /></a></li>
<li><a href="javascript:locationRefresh();"><img mouseover="true" src="../resource/images/mpm/button/toolbar_refresh.gif" alt="" /></a></li>
<li><a href="javascript:addFile('addFileForm','files');"><img mouseover="true" src="../resource/images/mpm/button/toolbar_addnew.gif" alt="" /></a></li>
<p> </p>
</ul>
</div>
<table width="100%" cellspacing="0">
<tr>
<td colspan="4" class="list-title">包信息</td>
</tr>
<tr>
<td width="15%" class="table-search-td-1">节点名称</td>
<td width="35%" class="table-search-td-2"><span class="td-3">
<select name="nodeId" id="nodeId" onchange="selectChange(this.options[this.selectedIndex].value);">
<logic:iterate id="scheduleInfor" name="scheduleInforList">
<option value="<bean:write name="scheduleInfor" property="id"/>">
<bean:write name="scheduleInfor" property="nodeName" /></option>
</logic:iterate>
<option value="-1">平级节点或临时</option>
</select>
</span></td>
<td width="15%" class="table-search-td-1">阶段名称</td>
<td class="table-search-td-2"><span class="td-3">
<select name="processId" id="processId">
</select>
</span></td>
</table>
<table width="100%" cellspacing="0" id="addFileForm">
<tr>
<td colspan="10" class="list-title">上传包文档</td>
</tr>
<tr><td><input type="file" name="addFileForm0" id="addFileForm0" class="smallInput0"/>
</td></tr>
</table>
<!--
<iframe name="attachFiles" width="100%" scrolling="no"
height="180" src="<c:url value='/itsm/showAttachmentAdd.do?attachType=13&bizID=0'/>0"
allowTransparency="true" frameborder="0" ></iframe>
-->
<table border="0" align="center" cellpadding="3" cellspacing="1"
width="100%">
<tr>
<td colspan="2" align="center" class="td-3" >
<a href="javascript:savaSubmit();"><img src="../resource/images/mpm/button/submit.gif" mouseover="true" alt="Submit" border="0" /></a>
<a href="javascript:historyBack();"><img src="../resource/images/mpm/button/back.gif" mouseover="true" alt="Back" border="0" /></a> </td>
</tr>
</table>
</div>
</div>
<table>
</div>
</div>
</html:form>
</body>
</html>
struts1.2:
List<FormFile> formFiles = new ArrayList<FormFile>();
Hashtable table = schema.getMultipartRequestHandler().getFileElements();
for(Iterator<String> iterator = table.keySet().iterator();iterator.hasNext();){
String name = iterator.next();
FormFile file = (FormFile)table.get(name);
if(null != file)
formFiles.add(file);
}