-
Sep 6th, 2008, 07:09 AM
#1
Explain code
hello reeyan,
i have seen ur multiple file uploading code on spring forum, code is working so thanks to help us.., but i want to know the code explanation can u help me for this.
i put ur code here..
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>WEBDAV File upload testing</title>
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">
<!--
// Multiple file upload
var total_rows = 1;
function add_upload(){ //3
if(total_rows < 10){
var tr, td;
var tbody = document.getElementById("tagTBody");
tr = tbody.insertRow(tbody.rows.length);
td = tr.insertCell(tr.cells.length);
td.innerHTML += "<input type='file' name='fileList["+total_rows+"]' size='25' \"><br>";
total_rows++;
alert("total rows :: " + total_rows);
} else {
alert("Max file upload allows less than 10");
}
}
function remove_upload() {
var tbody = document.getElementById("tagTBody");
if(tbody.rows.length > 1) {
tbody.deleteRow(tbody.rows.length - 1);
total_rows--;
alert("total rows :: " + total_rows);
} else {
alert("You cant not delete it!!! :: " + total_rows);
}
}
//-->
</script>
<form method="post" name="fileUploadForm" action="fileUpload.htm" enctype="multipart/form-data">
<a href="#" onClick="add_upload();">Add upload</a>
<a href="#" onClick="remove_upload();">Remove upload</a><!-- file attacthment table //-->
<table id="upload_file_table">
<tbody id="tagTBody">
<tr>
<td><input type='file' name='fileList[0]' size='25'></td>
</tr>
</tbody>
</table>
<input type="submit" value="Send"></form>
</body>
</html>
2. FileUploadBean
public class FileUploadBean {
private List<MultipartFile> fileList = new ArrayList<MultipartFile>();
public void addFileList(MultipartFile files) {
this.fileList.add(files);
}
public void setFileList(List<MultipartFile> fileList) {
this.fileList = fileList;
}
public List<MultipartFile> getFileList() {
return fileList;
}
}
3. Controller
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
FileUploadBean fileUploadBean = (FileUploadBean)command;
List<MultipartFile> files = fileUploadBean.getFileList();
log.debug("files size = " + files.size());
OutputStream out = null;
if(files.size() > 0) {
Iterator<MultipartFile> iter = files.iterator();
while(iter.hasNext()) {
MultipartFile file = iter.next();
String fileName = file.getOriginalFilename();
log.debug("fileName = " + fileName);
if(fileName != null && fileName.length() > 0) {
File outFile = new File(filePath + fileName);
log.debug("file path = " + filePath);
try {
out = new BufferedOutputStream(new FileOutputStream(outFile));
byte[] write = file.getBytes();
out.write(write, 0, write.length);
log.debug("written file : " + fileName);
} finally {
if(out != null) {
out.close();
}
}
}
}
}
return new ModelAndView(getSuccessView(), getCommandName(), command);
}
4. application-context.xml
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.C ommonsM ultipartResolver">
</bean>
<bean id="fileUploadController"
class="com.formulla.web.controller.FileUploadContr oller">
<property name="commandClass" value="com.formulla.web.bean.FileUploadBean" />
<property name="formView" value="fileUpload" />
<property name="successView" value="fileUpload" />
</bean>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules