问题
I am using blueimp fileupload plugin which works fine in chrome,although same is not working in IE less then IE10.I am getting a prompts to open or save json result.After searching i tried few solution. like one here:-https://github.com/blueimp/jQuery-File-Upload/wiki/Setup#content-type-negotiation ,but no luck.
I am using Spring3.1,AngularJS. with my 1st approach i am able to upload file,although getting prompts to open or save json in IE as result also
Content-Type application/json;charset=UTF-8output Json returned is
ExcelUpload={UnprocessedRequests=12,AcceptedRequests=0, processedFlag=T,RejectedRequests=0,result=Excel_13}
Spring code is
@RequestMapping(value = "/saveExcel", method = RequestMethod.POST)
public @ResponseBody Map<String, Object> saveExcelDetails(@RequestParam(value="file",required=false) MultipartFile file,HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, Object> excelUpload = myService.saveExcelDetails(request,file);
response.setHeader("Content-Type", "text/html; charset=utf-8");
return excelUpload;
}
then i tried 2nd approach
@RequestMapping(value = "/saveExcel", method = RequestMethod.POST,**produces = "text/html"**)
public @ResponseBody Map<String, Object> saveExcelDetails(@RequestParam(value="file",required=false) MultipartFile file,HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, Object> excelUpload = myService.saveExcelDetails(request,file);
return excelUpload;
}
but getting error as "Could not find acceptable representation" am i missing something here
Iframe based uploads require a Content-type of text/plain or text/html for the JSON response - they will show an undesired download dialog if the iframe response is set to application/json.we can make use of the Accept header to offer different content types for the file upload response.
how can i achieve
to make use of the Accept header to offer different content types for the file upload response.
in my scenario(code).
Eric i changed it as you suggested
public @ResponseBody Map<String, Object>saveExcelDetails(@RequestParam(value="file",required=false) MultipartFile file,HttpServletRequest request, HttpServletResponse response,@RequestHeader(value="Accept") String accept) throws Exception {
Map<String, Object> excelUpload = myService.saveExcelDetails(request,file);
if (accept.indexOf("application/json") != -1) {
response.setContentType("application/json; charset=UTF-8");
} else {
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type","text/plain; charset=UTF-8");
}
return excelUpload;
}
done setting response content type as "text/plain", also to the response Header still no luck.
回答1:
Try below codes:
<beans:bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<beans:property name="supportedMediaTypes">
<beans:list>
<beans:value>text/html;charset=UTF-8</beans:value>
</beans:list>
</beans:property>
</beans:bean>
<beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="mappingJacksonHttpMessageConverter" />
</beans:list>
</beans:property>
</beans:bean>
回答2:
By design, IE will prompt the user to save a file if you navigate a frame to a URL that returns Content-Type: application/json
as IE does not offer a native view for JSON.
In your server side code, you can examine the request's Accept
header. If it doesn't contain application/json
then you should return text/plain
instead.
回答3:
It could be because of the @ResponseBody annotation, I think that the content type gets reset to application/json after you set it to text/plain. The JacksonMessageConverter might have something to do with it.
I see in Fiddler2 that IE8 is not sending application/json in its Accept: header. At the server side, I check the Accept header if it contains application/json. (Btw I just use "contains()" rather than the clunky "IndexOf") and replace it with text/plain if application/json isn't in the Accept: header.
But sure enough, the response Fiddler2 gets to see states that it has a Content-Type header of 'application/json'.
So something is resetting the Content-Type, after I reset it to be text/plain.
I'm about to try this: http://incomplete-code.blogspot.be/2012/08/spring-mvc-internet-explorer-7-and-json.html
回答4:
IE actually does a MIME detection if header("X-Content-Type-Options", "nosniff");
were not set, and overwritten the servers response to IE detected format. For example, a JSON response with text/html
header will be overwritten with text/javascript
for security purpose.
Reference:
MIME Type Detection in Windows Internet Explorer
Handling MIME Types in Windows Internet Explorer
Handling MIME Types in Windows Internet Explorer
Wiki: MIME Sniffing
来源:https://stackoverflow.com/questions/21045522/ie-prompts-to-open-or-save-json-result-which-comes-from-server