jQuery-File-Upload和Spring MVC IE9不起作用

时间:2012-12-24 19:14:25

标签: jquery spring-mvc file-upload

我使用jQuery-File-Upload和Spring MVC就像服务器部分一样。我尝试the example,它在IE 9浏览器中不起作用。它告诉我:“你想保存或打开物体”

客户端就像示例中一样:

<!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>

服务器是:

@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
}

它适用于除IE之外的所有浏览器

2 个答案:

答案 0 :(得分:1)

我有完全相同的问题并设法通过更改方法来始终生成'text / plain'内容而不是'application / json'来解决它。我这样做是通过添加'produce =“text / plain”'来请求映射注释,将返回值类型更改为String并使用Jackson手动将对象转换为Json。

使用您的代码,这看起来像这样:

@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);
}

这似乎适用于IE9(使用iframe传输)和现代浏览器,但没有,但似乎有点hackish,因为正确的类型应该是'application / json'。

答案 1 :(得分:0)

您的问题听起来像这样:

IE prompts to open or save json result from server

希望有所帮助