Spring MVC ajaxfileupload File upload returns JSON download problem

Source: Internet
Author: User
Tags string to json

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.