SpringMVC + ajaxFileUpload solution for uploading images to IE browser via pop-up download box

Source: Internet
Author: User

SpringMVC + ajaxFileUpload solution for uploading images to IE browser via pop-up download box

 


For example, record the solution to this problem. The core cause is that ajaxfileupload does not support setting the Response Header ContentType to application/json, and IE does not support this format, when SpringMVC's @ ResponseBody annotation is used, the response type is automatically set to application/json, so the solution is to manually set the response type.

 

Step 1 discard @ ResponseBody, manually output the response through the stream, and specify the response type as text/html

 

@ RequestMapping (value = "/fileImport2") public Map
 
  
FileImport2 (HttpServletRequest request, HttpServletResponse response, MultipartHttpServletRequest req, @ RequestParam ("filename") MultipartFile uploadFile) {response. setContentType ("text/html; charset = UTF-8"); try {// File Upload String updateLoadPath = FileOperateUtil. uploadExcel (req, request, uploadFile); String publishPath = request. getSession (). getServletContext (). getRealPath ("/");//... response. getWriter (). write ("{\" data \ ": \" OK \ "}");} catch (Exception e) {e. printStackTrace (); try {response. getWriter (). write ("{\" data \ ": \" error \ "}");} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace ();}} return null ;}
 

Step 2 set dataType of $. ajaxFileUpload to text (otherwise, the value of the callback function is [object. object])

 

$. AjaxFileUpload ({url: postPath + "/app/studentScore/fileImport2", // secureuri: false, // whether the security protocol is required, generally set to false fileElementId: 'filename', // The ID dataType: 'text' of the file upload field, // the return value type is generally set to json success: function (data) // The server responds to the processing function {alert (data); var repObj = $. parseJSON (data); if (repObj. data = 'OK') {alert ("imported successfully! ");} Else {alert (" Data Exception! ");}}});

 

After the problem is solved, let's take a brief look at the debugging information of FireBug:

 

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.