Using jquery. Upload. js asynchronous upload sample code

  • 2020-03-30 03:37:55
  • OfStack

The related resources download: (link: http://xiazai.jb51.net/201407/yuanma/upload (jb51.net). Zip)

1: the JSP code:

Import jquery. Upload. Js and jquery-1.7.2.js

Add code to call js: < A href="javascript:void(0)" rel="external nofollow" onclick="doUpload()"> Upload < / a>

At the bottom:


<iframe style="position:absolute;top:-9999px" src="<%=basePath%>/resources/upload/upload.htm"/></iframe>

2: js code:



function doUpload() {
//Upload method
$.upload({
//Upload the address
url:window.basePath+'/reply/imageUpload', 
//File name word
fileName: 'uploadfile', 
//Other form data
params: {},
//When the upload is complete, return json, text
dataType: 'json',
//Callback before upload,return true means continue to upload
onSend: function() {
return true;
},
onSubmit: function(){
},
//Callback after upload
onComplate: function(data) {
if(data.msg){

}else{
alert(" Error uploading picture! ");
}
}
});
}

3: background code:



@RequestMapping(value="imageUpload")
@ResponseBody
public Object imageUpload(HttpServletRequest request,HttpServletResponse response){
Map<String, Object> map=new HashMap<String, Object>();
UploadUtil uploadUtil = new UploadUtil();
try {
String url = uploadUtil.excelUpload(request, response);//File upload
} catch (IOException e) {
e.printStackTrace();
}
return map;
}

Related articles: