简体   繁体   English

处理程序执行导致异常:所需的MultipartFile参数'file'不存在

[英]Handler execution resulted in exception: Required MultipartFile parameter 'file' is not present

My Controller code : 我的控制器代码:

@RequestMapping(value = "/rest/auth/admin/test/responseTest", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<ResponseVO> responseTest(@RequestParam("file") MultipartFile file,
        @RequestParam(value = "testId", required = true) long testId) {

I have already added the multipartResolver bean to my application-context.xml 我已经将multipartResolver bean添加到了application-context.xml中

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

My javascript code : 我的JavaScript代码:

var fd = new FormData();
fd.append('file', $('input[type=file]')[0].files[0]);
fd.append("label", "WEBUPLOAD");
var headers = {};
headers[Constants.XCSRFTOKENCookieName] = util.getCookie(Constants.XCSRFTOKENCookieName);
var url = "rest/auth/admin/test/responseTest?responseTest=123";
var dataType = "json";
var contentType = "application/json";
$.ajax({
    url: url,
    type: "POST",
    data: fd,
    dataType: dataType,
    async: isAsync,
    headers: headers,
    enctype: contentType,
    processData: false, // tell jQuery not to process the data
    contentType: false, // tell jQuery not to set contentType
    success: function(result, status, jqXHR) {
        resultObj = result;
        if (successCallBack != null) {
            successCallBack(resultObj);
        }
        //util.hideLoader();
    },
    error: function(jqXHR, textStatus, errorThrown) {
        console.log("Error : " + errorThrown);
        resultObj = jqXHR.responseJSON;
        if (errorCallBack != null) {
            errorCallBack(resultObj);
        }
        //util.hideLoader();
    }
});

When I am calling the above Ajax I am getting the following error at server side. 当我调用上述Ajax时,在服务器端出现以下错误。

[org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] > logException() : 186 - Handler execution resulted in exception: Required MultipartFile parameter 'file' is not present

Please help me to resolve this issue. 请帮助我解决此问题。

我本以为contentType标头应该是multipart / form-data而不是application / json?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM