多文件選擇上傳&使用Spring框架
在這篇文章中我描述了多文件上傳服務器端和客戶端的代碼。
下面的代碼是在appContext.xml
提多數據
appContext.xml
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="20971520"/>
</bean>
Simpleupload.jsp:
腳本多文件上傳:
<script type="text/javascript">
var totalsizeOfUploadFiles = 0;
function getFileSizeandName(input)
{
var select = $('#uploadTable');
for(var i =0; i<input.files.length; i++)
{
var filesizeInBytes = input.files[i].size;
var filesizeInMB = (filesizeInBytes/(1024*1024)).toFixed(2);
var filename = input.files[i].name;
//alert("File name is : "+filename+" || size : "+filesizeInMB+" MB || size : "+filesizeInBytes+" Bytes");
if(i<=4)
{
$('#filetd'+i+'').text(filename);
$('#filesizetd'+i+'').text(filesizeInMB);
}
else if(i>4)
select.append($('<tr id=tr'+i+'><td id=filetd'+i+'>'+filename+'</td><td id=filesizetd'+i+'>'+filesizeInMB+'</td></tr>'));
totalsizeOfUploadFiles += parseFloat(filesizeInMB);
$('#totalsize').text(totalsizeOfUploadFiles.toFixed(2)+" MB");
if(i==0)
$('#filecount').text("1file");
else
{
var no = parseInt(i) + 1;
$('#filecount').text(no+"files");
}
}
}
function CloseAndRefresh()
{
opener.location.reload(true);
self.close();
}
</script>
HTML表單設計:
<body>
<form method="post" id="uploadForm" action="upload" enctype="multipart/form-data">
<table class="span10">
<tr>
<td colspan="3">
<legend>Simple Upload</legend>
</td>
</tr>
<tr>
<td>
<input type="file" name="files[]" multiple="multiple" onchange="getFileSizeandName(this);"/>
</td>
</tr>
<tr>
<td colspan="3">
<div id="uploaddiv">
<table id="uploadTable" class="table table-striped table-bordered">
<tr>
<th>Title</th>
<th>Size</th>
</tr>
<tbody id="tbodyid">
<tr id="tr0">
<td id="filetd0" height="10px" width="50px"></td>
<td id="filesizetd0" height="10px" width="5px"></td>
</tr>
<tr id="tr1">
<td id="filetd1"></td>
<td id="filesizetd1"></td>
</tr>
<tr id="tr2">
<td id="filetd2"></td>
<td id="filesizetd2"></td>
</tr>
<tr id="tr3">
<td id="filetd3"></td>
<td id="filesizetd3"></td>
</tr>
<tr id="tr4">
<td id="filetd4"></td>
<td id="filesizetd4"></td>
</tr>
</tbody>
<tfoot>
<tr>
<td id="filecount"></td><td id="totalsize"></td>
</tr>
</tfoot>
</table>
</div>
</td>
</tr>
<tr>
<td colspan="3">
<button class="btn btn-primary" type="submit" id="startButton" onClick="CloseAndRefresh();">Start</button>
<button class="btn" id="cancelButton">Cancel</button>
</td>
</tr>
</table>
</form>
UploadController.java代碼:如果使用多個文件上傳
<input type="file" multiple="true" />
另請參閱[在php文件上傳中選擇多個文件](http:// stackoverflow。com/questions/2071505/select-multiple-files-in-file-upload-in-php) – rds 2012-01-12 10:12:06
有很多像http://www.uploadify.com/這樣的jQuery插件可以讓你完成多個文件上傳。 – 2011-12-29 15:18:39