2014-10-31 83 views
0

我有一個控制器和一個上傳文件的方法,在客戶端使用支持除IE以外的所有瀏覽器的Ajax上傳的dojo Uploader類,並上傳與IE的IFrame。 結果是一個JSON對象,但在使用的IFrame機構的情況下,JSON必須在< textarea的>括:HTTP請求結果的不同編碼取決於Accept標頭

@RequestMapping(value = "/documentation/{appId:.+}/", method = RequestMethod.POST) 
@ResponseBody 
public String uploadDocumentation(HttpServletRequest request, 
     @PathVariable String appId, @RequestParam("uploadedfile") MultipartFile file) 
     throws Exception { 
    // .... 
    String json = JsonUtils.jsonify(map); 
    if (accepts(request, "application/json")) { 
       return json; 
    } else if (accepts(request, "text/html")) { 
     return "<textarea>" + json + "</textarea>"; 
    } else { 
     throw new GinaException("Type de retour non supporté"); 
    } 

我想知道是否有登記在框架這個編碼機制方式,所以我們只需要返回一個對象,然後讓框架完成剩下的工作。

在此先感謝。

回答

0

對於記錄,我簡單地加入的第二方法:

@RequestMapping(value = "/documentation/{appId:.+}/", method = RequestMethod.POST, 
     produces="application/json") 
@ResponseBody  
public UploadResult uploadDocumentation(@PathVariable String appId, 
     @RequestParam("uploadedfile") MultipartFile file) throws Exception { 
    ... 
    return new UploadResult(filename); 
} 

@RequestMapping(value = "/documentation/{appId:.+}/", method = RequestMethod.POST, 
     produces="text/html") 
@ResponseBody  
public String uploadDocumentationIE(@PathVariable String appId, 
     @RequestParam("uploadedfile") MultipartFile file) throws Exception { 
    UploadResult obj = uploadDocumentation(appId, file); 
    String json = JsonUtils.jsonify(obj); 
    return "<textarea>" + json + "</textarea>"; 
}