简体   繁体   中英

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

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

My javascript code :

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.

[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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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