Jquery multipart请求不适用于Spring MVC服务器

时间:2016-07-24 13:18:53

标签: jquery ajax spring-mvc

我正在尝试将ajax multipart表单上传到Spring MVC服务器。但是,看起来我做错了。

这是我的客户代码:

var $form = $("#upload-form"),
            url = $form.attr( 'action' );

var params = {};
$.each($form.serializeArray(), function(i, field) {
     params[field.name] = field.value;
});

var data = new FormData();
var file = $("#file-input");
data.append('file', file);

$.ajax({
     // url:url,
     url:url + "?type=" + params.type + "&text=" + params.text,
     type:"POST",
     data:data,
     contentType: false,
     mimeType: "multipart/form-data",
     processData: false,
     success: function(data) {
          console.log('received response');
          // self.hide()
     },
     failure: function(err) {
          alert(err);
     }
})

另一方面,这是我的服务器代码。

@RequestMapping(method = RequestMethod.POST)
public ResponseEntity createPost(@RequestParam("text") String text,
                                 @RequestParam("type") String type,
                                 @RequestBody MultipartFile file, Principal principal) {
    //blah blah

    return response.ok();
}

此外,Chrome中记录的请求的有效负载是

------WebKitFormBoundary1L9rKeQBA9CmaBEC
Content-Disposition: form-data; name="file"

[object Object]
------WebKitFormBoundary1L9rKeQBA9CmaBEC--

我做错了什么?

0 个答案:

没有答案
相关问题