spring mvc ajaxfileupload文件上传返回json下载问题

问题:使用spring mvc ajaxfileupload 文件上传在ie8下会提示json下载问题

解决方案如下:

服务器代码:

    @RequestMapping(value = "/addAnalysis", method = RequestMethod.POST)
    public void addAnalysisUI(
            HttpServletResponse response,HttpServletRequest request,HttpSession session,
            @RequestParam(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 = new ResponseMessage();
        try {
            //校验信息
            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 analysis = new Analysis(analysisTitle, selectWeekly,
                        user.getName(), analysisSource, fileTemp.getName(),
                        new Date(), contents);
                analysisService.insertDataAnalysis(analysis);// 往数据库中插入一条记录
                message.setStatus(200);
            }
            out = response.getWriter();  
            out.append(JacksonUtils.obj2json(message)); 
        } catch (Exception ex) {
            logger.error(ex.getMessage());
        } finally {  
            if (out != null) {  
                out.close();  
            }  
        }  
    }

在我的项目里面配置jackjson转换器,后台返回json就不能用@responseBody 配置的Jackson 适配转换器转化,只能自己手动构建json字符串,设置Content-Type为“text/html;chartset=UTF-8”,然后通过response输出,因为在ie不支持application/json格式,另外firefox, chrome浏览器iframe在接收application/json格式的时候会自动将其转化为html格式,自动在json数据前后加上<pre></pre>标签导致ajaxfileupload插件取json数据错误

前端js代码如下:

function submitForm() {
    var oEditor = CKEDITOR.instances.contents;
    $('#contents').val(oEditor.getData());
    $('#form1').ajaxSubmit({
        url : 'XX/XX',
        dataType : 'text',
        cache : false,// 是否缓存
        type : 'POST',// 提交方式 可以选择post/get 推荐post
        async : false,// 同步异步 false是同步
        success : function(data) {
            var dataStr = $.parseJSON(data);//将String字符串转换成json
            alert(JSON.stringify(dataStr));
            if (dataStr.status == 200) {
                alert("新增成功!");
                loadApplication('analysis/index');
            } else {
                alert("新增失败!");
                loadApplication('analysis/index');
            }
        }
    });
}

 

posted @ 2015-07-09 12:08  简简-单单  阅读(750)  评论(0编辑  收藏  举报