For a form with a file upload, the background uses the SPRINGMVC encapsulated Multipartfile file to receive files, and you need to use asynchronous commit, and return the appropriate prompt information
Use jquery's form plug-in, which is jquery.form.js the plugin, and then use the plugin's Ajaxsubmit method;
1$ ("#pageForm"). Ajaxsubmit ({2Type: "POST",3URL: "Your action.html",4DataType: "JSON",5Successfunction(data) {6 if(data.msg== ' SUCCESS '){7 alert (success);8 }9 Else{Ten alert (data.msg); One } A } -});
1@RequestMapping ("/insertgoodsbrand")2 Public voidInsertgoodsbrand (3 httpservletrequest Request,4@RequestParam (value = "File", required =false) multipartfile file,httpservletresponse response,5 Goodsbrand Goodsbrand) {6Jsonobject obj =NewJsonobject ();7PrintWriter out =NULL;8 Try {9out =Response.getwriter ();TenString Path =request.getsession (). Getservletcontext (). Getrealpath ( One"Upload\\goods\\brand"); AString FileName =file.getoriginalfilename (); -String savefilename = Common.getnowcorrect2second () + "." -+ filename.substring (Filename.lastindexof (".") + 1); theFile targetfile =NewFile (path, savefilename); - if(!targetfile.exists ()) { - targetfile.mkdirs (); - } +String msg = ""; - File.transferto (targetfile); +msg =Backgoodsbrandbus.insertgoodsbrand (Goodsbrand, savefilename); A if(Msg.equals ("SUCCESS")) { atObj.put ("MSG", msg); -}Else { -Obj.put ("MSG", msg); - } - out.print (obj); -}Catch(Exception e) { in e.printstacktrace (); - } to finally{ + out.close (); - } the}
Jqueryajax uploading files using Multipartfile in SPRINGMVC