上传文件返回前端json的时候,去除返回值带 <pre style="word-wrap:break-word;white-space:prewrap;"></pre>的问题

------------恢复内容开始------------

方法一 :

后台用- response.getWriter(); 用out.write(obj)返回,还要添加response.setContentType("text/html");

 1 @RequestMapping("/cyyyye/apply/DataCyyyyyyAction!insyyyyuld.action")
 2     public void insyyyyuld(HttpServletRequest request, HttpServletResponse response, @RequestParam("uplodaFileId") MultipartFile upFile){
 3         PrintWriter out = null;
 4         JSONObject obj = new JSONObject();
 5         TempSaveUploadFileVo tempFileVo = null;
 6         try {
 7             response.setCharacterEncoding("utf-8");  
 8             out = response.getWriter();
 9             tempFileVo = upload(request, upFile);
10             obj.put("code", 200);
11             obj.put("mouldId", tempFileVo.getOid());
12             obj.put("mouldName", tempFileVo.getOriginal());
13             out.write(obj.toString());
14             response.setContentType("text/html"); /*返回前端json的时候,去除返回值带 <pre style="word-wrap:break-word;white-space:prewrap;"></pre>的问题*/
15             out.flush();
16             out.close();
17         } catch (Exception e) {
18             logger.error(e);
19             if(tempFileVo != null){
20                 File file = new File(tempFileVo.getDiskPath());
21                 if(file.exists()) file.delete();
22             }
23         } finally{
24             if(out != null) out.close();
25         }
26     }

 前端的请求

function insertMould(obj){
    if($(obj).val() == ''){
        return;
    }
    
    var suffix = getFileSuffix($(obj).val()).toLowerCase();
    if(suffix != 'xls' && suffix != 'xlsx'){
        tipfadeinout('<div style="text-align:center;color:red;">请选择Excel文档!</div>', 200, 500);
        $(obj).val('');
        return;
    }
    var taskId = $("#taskId").val();
    var circleId = $("#circleId").val();
    var sendData = {'taskId' : taskId, 'circleId' : circleId};
    var url = getRootPath() + '/cyyyye/apply/DataCyyyyyyAction!insyyyyuld.action?taskId='+taskId;
    $.ajaxFileUpload({  
        type: "POST",  
        url: url,  
        data: sendData,
        secureuri : false,  //是否启用安全提交,默认为false  
        fileElementId: 'uplodaFileId',   //文件选择框的id属性  
        dataType: 'JSON',
        async : false,  
        success: function(data){
            var vo = eval('('+ data +')');
            if(vo.code == 200){
                $("#localMouldId").val(vo.mouldId);
                $("#editorMould").show();
                $("#editorMould").children().eq(0).val(vo.mouldName);
                $("#upFile").hide();
                saveaddbtn();
            }
        }
    });
}

 

方法二:

后台用@ResponseBody

前台接送json用

 success: function(data){
            var vo = jQuery.parseJSON(jQuery(data).text());

 

posted @ 2020-05-22 14:00  abcdjava  阅读(716)  评论(0编辑  收藏  举报