Issue: Uploading with spring MVC ajaxfileupload file under IE8 will prompt for JSON download issues
The solution is as follows:
Server code:
@RequestMapping (value = "/addanalysis", method =requestmethod.post) Public voidAddanalysisui (HttpServletResponse response,httpservletrequest request,httpsession session, @Re Questparam (Required=false, value = "File") multipartfile file, @RequestParam (required=false, value = "Analysistitle") String Analysistitle, @RequestParam (required=true, value = "Selectweekly") String selectweekly, @RequestParam (required=false, value = "Contents") String contents, @RequestParam (Required=true, value = "Analysissource") (String analysissource) {response.setcharacterencoding ("UTF-8"); Response.setcontenttype ("Text/html"); PrintWriter out=NULL; Responsemessage message=NewResponsemessage (); Try { //Verifying information if(Stringutils.isblank (Analysistitle) && Stringutils.isblank (selectweekly) && Stringutils.isblank ( Analysissource) && File = =NULL) {Message.setstatus (202); } Else{String Savepath= Request.getservletcontext (). Getrealpath ("/web-inf/upload"); Manageraccount User=(Manageraccount) session. getattribute (Constants.current_user); File filetemp=filepersistence (savepath, file); Analysis=NewAnalysis (Analysistitle, selectweekly, User.getname (), Analysissource, Filetemp.getname (), NewDate (), contents); Analysisservice.insertdataanalysis (analysis);//insert a record into the databaseMessage.setstatus (200); } out=Response.getwriter (); Out.append (Jacksonutils.obj2json (message)); } Catch(Exception ex) {Logger.error (Ex.getmessage ()); } finally { if(Out! =NULL) {out.close (); } } }
Configure the Jackjson converter in my project, back to JSON in the background can not be converted with the @responsebody configuration of the Jackson adapter converter, can only manually build the JSON string, set Content-type to "text/html; Chartset=utf-8 "and then through response output, because in IE does not support Application/json format, in addition Firefox, Chrome browser iframe in receive application/ JSON format will automatically convert it to HTML format, automatically before and after the JSON data with <pre></pre> tags cause the ajaxfileupload plugin to take JSON data errors
The front-end JS code is as follows:
function SubmitForm () {var oeditor=CKEDITOR.instances.contents; $(' #contents '). Val (Oeditor.getdata ()); $(' #form1 '). Ajaxsubmit ({URL:' Xx/xx ', DataType:' Text ', Cache:false,//whether to cacheType: ' POST ',//Submit method can choose Post/get recommended postAsync:false,//synchronous asynchronous false is synchronoussuccess:function (data) {var datastr= $.parsejson (data);//Convert string string to JSONalert (json.stringify (DATASTR)); if(Datastr.status = = 200) {alert ("New success!"); Loadapplication (' Analysis/index '); } Else{alert ("New failed!"); Loadapplication (' Analysis/index '); } } });}
Spring MVC ajaxfileupload File upload returns JSON download problem