簡體   English   中英

處理程序執行導致異常:所需的MultipartFile參數'file'不存在

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

我的控制器代碼:

@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) {

我已經將multipartResolver bean添加到了application-context.xml中

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

我的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();
    }
});

當我調用上述Ajax時,在服務器端出現以下錯誤。

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

請幫助我解決此問題。

我本以為contentType標頭應該是multipart / form-data而不是application / json?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM