Spring Boot and jQuery file upload : Unsupported media type

I am trying to upload a csv file with spring boot 1.2.0 REST controller and jQuery ajax. When I send the post request, I keep getting 415:Unsupported Media type error. Here is my form:

<form id="upload_form">
    <div id="message">        

    </div>

    <br/>
    <div class="row" id="upload-file-div" style="display: none">
        <div class='col-sm-3'>
            <label>Select File</label>
            <input type="file" name="file">
        </div>

         <div class='col-sm-3'>
             <input type="button" id="file-upload" class="btn btn-primary" value="Upload" onclick="uploadFile()"/>           
        </div>
    </div>
</form>

Here is my method to upload file:

function uploadFile(){
    var response = api.uploadCSV($("#upload_form"));
    if(response.status === 'OK'){
        $("#message").css('color', 'green');
    }else{
        $("#message").css('color', 'red');
    }
    $("#message").html(response.message);
}

Here is the actual jQuery POST:

upload: function (url, form, ignoreSuccess) {
    var response = null;
    if (!this.validate(form)) {
        var array = form.serializeArray();
        alert(array);
        var formData = new FormData(form);
        console.warn(formData);
        $.ajax({
            type: "POST",
            url: API_PROXY + url,
            data: formData,
            cache: false,
            contentType: false,
            processData: false,
            async: false,
            beforeSend: function (request) {
                if (api.getSession() !== null) {
                    request.setRequestHeader("Authorization", "Bearer " + api.getSession().bearer);
                }
            },
            success: function () {}

        }).done(function (msg) {
            response = msg;
        });

    }
    return response;
}

Following is my controller:

@RequestMapping(consumes = "multipart/form-data", method = RequestMethod.POST, 
        value = "/upload/twitter", produces = MediaType.APPLICATION_JSON_VALUE)
public Response<String> uploadCsv(CsvUploadModel form) {

//Code

}

I have both MultipartConfigElement and MultipartResolver annotated in my spring boot class. I am using spring boot 1.2.0. When I send the post request with PostMan (chrome extension), it works as expected. However, when I try the above jquery code, it keeps throwing unsupported media type error.

Following things I have tried:

  • Playing around content-type header, finally I have set the contentType to false.
  • Using form.serializeArray(), iterating over it and appending individual elements into formData.
  • Sending the form object instead of form data.
  • Can anyone please help me with this? Thanks in advance.


    You can append your file input in your formData ; the following changes needs to be made:

    This:

    <input type="file" name="file">
    

    Should be:

    <input type="file" name="file" id="file">
    

    And in your ajax code, this:

    var formData = new FormData(form);
        console.warn(formData);
        $.ajax({
            type: "POST",
            url: API_PROXY + url,
            data: formData,
    

    Should be:

    var formData = new FormData();
    formData.add("file",$('#file')[0].files)
        $.ajax({
            type: "POST",
            url: API_PROXY + url,
            data: formData,
    

    Or:

    var formData = new FormData(form[0]);
    $.ajax({
            type: "POST",
            url: API_PROXY + url,
            data: formData,    
    
    链接地址: http://www.djcxy.com/p/8576.html

    上一篇: 无法将文件从Angular客户端上传到Spring Java服务器:说错误400

    下一篇: Spring Boot和jQuery文件上传:不支持的媒体类型