2

This might be simple, but I really can't wrap my head around it right now.
FormData and File reach controller as expected, service process continues without any exception, yet in Browser I get a Status Code: 404 Not Found what could be the reason? Any ideas?

The Ajax call

$.ajax({
    type: 'POST',
    contentType: false,
    processData: false,
    url: '/upload-form-and-attachment/',
    data: formData,
    dataType: 'text',
    success: function(response, textStatus) {
        displayPNotifyMessage(textStatus, '<spring:message code="something.success"/>'.format([response.operationData]), 'success');
    },
    error: function($data, textStatus, errorThrown) {
        displayPNotifyMessage(textStatus, errorThrown, 'error');
    },
    complete: function(){
        closeDialog();
    }
});

The formData

    var formData = new FormData();        
    formData.append('entityIds', $('#theDialog').data('brp').selectedEntityIds);
    formData.append('description', $("#description").val());
    formData.append('modeIds', $('select#otherEntityIds').val());
/*Here I append more fields to the form, and finally I append the input type file*/
    formData.append('attachment', $('input#attachment')[0].files[0]);

The controller

  @RequestMapping(value = "/upload-form-and-attachment/", method = RequestMethod.POST)
    public JSONResult uploadFormAndAttachment(
            @RequestParam("entityIds") List<Long> entityIds,
            @RequestParam("description") String description,
            @RequestParam("modeIds") List<Long> modeIds,
            MultipartHttpServletRequest request,
            HttpSession session) {          
        JSONResult jsonResult = new JSONResult();
        try {
            MultipartFile attachment = request.getFile("attachment");
            // Calling some service methods and passing them the form data
        } catch(Exception e) {
            LOGGER.error(e.getMessage());
        }       
        return jsonResult;
    }

The Request Headers as they appear in Chrome console

POST /***/upload-form-and-attachment/ HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 27274
Accept: text/plain, */*; q=0.01
Origin: http://localhost:8080
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryJqb7gbJhzfXRBRX7
Referer: http://localhost:8080/****
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cookie: JSESSIONID=*******

The Response Headers in Chrome console

Cache-Control: no-store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Content-Language: en
Content-Type: text/html;charset=UTF-8
Date: Sat, 20 Apr 2019 05:24:22 GMT
Expires: Sat, 6 May 1995 12:00:00 GMT
Pragma: no-cache
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block

Changing contentType to 'multipart/form-data'
The obvious solution of changing contentType to 'multipart/form-data' as the answer here suggests fails and produces the following Error in API

org.springframework.web.multipart.MultipartException: 
Failed to parse multipart servlet request; 
nested exception is org.apache.commons.fileupload.FileUploadException: 
the request was rejected because no multipart boundary was found

Changing contentType to 'application/x-www-form-urlencoded'
Using this content type causes the following Error in API, since the whole request is apparently "stringified" and API fails to recognize the parameters passed.

org.springframework.web.bind.MissingServletRequestParameterException:  
Required List parameter 'entityIds' is not present
Christos Karapapas
  • 1,018
  • 3
  • 19
  • 40

0 Answers0