jQuery-File-Upload and Spring MVC IE9 doesn't

2019-08-07 15:14发布

问题:

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

回答1:

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'.



回答2:

Your question sounds like this:

IE prompts to open or save json result from server

hope it helps