<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<!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>AjaxUpload</title>
<script type="text/javascript">
function saveResult(sMessage) {
var divStatus = document.getElementById("divStatus");
divStatus.innerHTML = "File uploaded: " + sMessage;
document.messageForm.message.value = sMessage;
}
</script>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="upload.jsp" target="hidden_frame" >
<input name="file" type="file"/>
<input type="submit" value="上传"/>
</form>
<form name="messageForm">
<input name="message" value=""/>
</form>
<iframe name="hidden_frame" style="display:none"></iframe>
<div id="divStatus"></div>
</body>
</html>
AjaxUpload.jsp, 通过隐藏的iframe传递数据
upload.jsp, 需要jspSmartUpload组件的支持
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page import="com.jspsmart.upload.SmartUpload"%>
<!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>
<%
SmartUpload su = new SmartUpload();
su.initialize(pageContext);
su.setTotalMaxFileSize(20*1024*1024);
boolean sign = true;
String msg = "";
try {
su.upload();
su.save("D:/UploadedFiles");
} catch (Exception e) {
e.printStackTrace();
sign = false;
}
if(sign==true) {
msg = "OK!";
} else {
msg = "Failed!";
}
%>
<script type="text/javascript">
window.onload = function () { parent.saveResult("<%= msg %>"); };
</script>
</head>
<body>
</body>
</html>