Page code:
<!--introduction of related JS files, relative paths--
<script type= "Text/javascript" src= "Js/jquery.js" ></script>
<script type= "Text/javascript" src= "js/Ajaxfileupload. js "></script>
<!--functions to perform the upload file operation--
<script type= "Text/javascript" >
functionAjaxfileupload(){
$.Ajaxfileupload(
{
URL: ' Update.do?method=uploader ',//link to server address
Secureuri:false,
Fileelementid: ' Housemaps ',//File selection Box id attribute
DataType: ' xml ',//The format returned by the server, can be JSON
Success:function (data, status)//equivalent to the use of a try statement block in Java
{
$ (' #result '). HTML (' Add success ');
},
Error:function (data, status, E)//equivalent to the use of Catch statement blocks in Java
{
$ (' #result '). HTML (' Add failed ');
}
}
);
}
</script>
<body>
<form method= "POST" action= "Update.do?method=uploader" enctype= "Multipart/form-data" >
<input type= "File" id= "Housemaps" name= "Housemaps"/>
<input type= "button" value= "Submit" onclick= "Ajaxfileupload() "/>
</form>
<div id= "Result" ></div>
</body>
Server code:
public class Updateaction extends Dispatchaction {
Public Actionforward Uploader (actionmapping mapping, Actionform form,
HttpServletRequest request, HttpServletResponse response) {
Upformform upformform = (upformform) Form;
Formfile ff = Upformform.gethousemaps ();
try {
InputStream is = Ff.getinputstream ();
File File = new file ("d:/" + ff.getfilename ()); Specify the path and file name of the files store
OutputStream OS = new FileOutputStream (file);
Byte[] B = new byte[1024];
int len = 0;
while (len = Is.read (b))! =-1) {
Os.write (b, 0, Len);
}
Os.close ();
Is.close ();
} catch (Exception e) {
E.printstacktrace ();
}
return null;
}
}
Go: A simple jquery plugin ajaxfileupload implementation Ajax upload file example