jQuery的文件上传和春天MVC IE9不起作用 [英] jQuery-File-Upload and Spring MVC IE9 doesn't work
问题描述
我使用 jQuery-File-Upload 和Spring MVC之类的服务器部分。我尝试这个例子,它在IE中不起作用9浏览器。它对我说:你想保存还是打开对象?
客户端就像这样:
<!DOCTYPE HTML>
< html>
< head>
< meta charset =utf-8>
< title> jQuery文件上传示例< / title>
< / head>
< body>
< input id =fileuploadtype =filename =files []data-url =/ fileUpload / 1multiple>
< script src =// ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js\"> ;</script>
< script src =js / vendor / jquery.ui.widget.js>< / script>
< script src =js / jquery.iframe-transport.js>< / script>
< script src =js / jquery.fileupload.js>< / script>
< script>
$(函数(){
$('#fileupload')。fileupload({
dataType:'json',
完成:function(e,data){
$ .each(data.result.files,function(index,file){
$('< p />').text(file.name).appendTo(document.body);
});
}
});
});
< / script>
< / body>
< / html>
而服务器是:
@RequestMapping(value =fileUpload / {id},method = RequestMethod.POST)
@ResponseBody
Public FileUploadResultDTO upload(MultipartFile file,
@PathVariable Long id,
语言环境语言环境,
HttpServletRequest请求){
FileUploadResultDTO resultDTO = new FileUploadResultDTO();
//逻辑保存文件并返回响应,如对象
return resultDTO;
}
public class FileUploadResultDTO {
private Long photoId;
私人字符串pathToPhoto;
私人字符串pathToSmallPhoto;
$ b $ // getters / setters
}
所有的浏览器,除了IE浏览器之外我还有一个完全相同的问题,并设法通过改变方法来解决这个问题,总是产生文本/文本/文本普通内容而不是应用程序/ json。我通过添加'produce =text / plain'来请求映射注释,将返回值类型更改为String并使用Jackson将对象手动转换为Json。
你的代码如下所示:
@ RequestMapping(value =fileUpload / {id},method = RequestMethod.POST,产生=text / plain)
@ResponseBody
public String upload(MultipartFile file,
@PathVariable Long id,
语言环境语言环境,
HttpServletRequest请求){
FileUploadResultDTO resultDTO = new FileUploadResultDTO();
//逻辑保存文件并返回响应,如对象
return Jackson.toJsonString(resultDTO);
$ b这些似乎适用于IE9(使用iframe传输)和现代浏览器没有,但似乎有点hackerish,正确的类型应该是应用程序/ JSON。
I use jQuery-File-Upload and Spring MVC like server part. I try the example and it doesn't work in the IE 9 browser. It says to me: "would you like to save or open the object"
Client is like in the example:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>jQuery File Upload Example</title>
</head>
<body>
<input id="fileupload" type="file" name="files[]" data-url="/fileUpload/1" multiple>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="js/vendor/jquery.ui.widget.js"></script>
<script src="js/jquery.iframe-transport.js"></script>
<script src="js/jquery.fileupload.js"></script>
<script>
$(function () {
$('#fileupload').fileupload({
dataType: 'json',
done: function (e, data) {
$.each(data.result.files, function (index, file) {
$('<p/>').text(file.name).appendTo(document.body);
});
}
});
});
</script>
</body>
</html>
And server is:
@RequestMapping(value = "fileUpload/{id}", method = RequestMethod.POST)
@ResponseBody
public FileUploadResultDTO upload(MultipartFile file,
@PathVariable Long id,
Locale locale,
HttpServletRequest request) {
FileUploadResultDTO resultDTO = new FileUploadResultDTO();
// Logic saving file and return back response like object
return resultDTO;
}
public class FileUploadResultDTO {
private Long photoId;
private String pathToPhoto;
private String pathToSmallPhoto;
// getters/setters
}
It works in all browsers except IE
解决方案 I had the exact same problem and managed to solve it by changing the method to always produce 'text/plain' content instead of 'application/json'. I did this by adding 'produces="text/plain"' to request mapping annotation, changing the return value type to String and converting the object manually to Json using Jackson.
With your code this would look something like this:
@RequestMapping(value = "fileUpload/{id}", method = RequestMethod.POST, produces="text/plain")
@ResponseBody
public String upload(MultipartFile file,
@PathVariable Long id,
Locale locale,
HttpServletRequest request) {
FileUploadResultDTO resultDTO = new FileUploadResultDTO();
// Logic saving file and return back response like object
return Jackson.toJsonString(resultDTO);
}
This seems to work for both IE9 (that uses the iframe transport) and modern browsers that do not, but seems as bit hackish,as the correct type should be 'application/json'.
这篇关于jQuery的文件上传和春天MVC IE9不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!