2

In my application I need to upload a file and along with that I need to send one more parameter. This is my code for the client side.

$.ajax({
                url : "/uploadFile",
                type : "POST",

                data: {file_name:new FormData($("#upload-file-form")[0]),"groupName":"xxx"},
                enctype : 'multipart/form-data',
        //      processData : false,
                contentType : false,
                cache : false,

                success : function(data) {
                    if (data == "success") {
                        // alert ("file uploaded successfully")

                        $(".modal-body").html(
                                "<p>File uploaded Successfully</p>")
                        $('#myModal').modal('show');

                    }
                    if (data == "failure") {
                    alert("failure)
                    }
               }
        }

My code for server side :

public String handleFileUpload(@RequestParam(value="file_name") MultipartFile file ,@RequestParam(value="groupName") String name){


    System.out.println("file");
    return "success";
}

But it says the current request is not a MultiPart Request. org.springframework.web.multipart.MultipartException: The current request is not a multipart request

How should I parse both file name and the other parameter? please help

My html form.

<form id="upload-file-form">
  <label for="upload-file-input">Upload your CSV file:</label>
  <input id="upload-file-input" type="file" name="uploadfile" accept="*" class="align-right" />
  <br>

  <input type="submit"  id ="groupUpload" value="click here to upload the file" class="btn btn-info" >
</form>

1 Answer 1

1

See you need to append the values in the FormData and it is the only thing needed to be passed. So you can append the extra value which needs to be passed like this:

var fd = new FormData($("#upload-file-form")[0]);
    fd.append('groupName', 'xxx');

now in the ajax you can simply pass this:

data: fd,
processData : false,  // <----required to upload
contentType : false,  // <----required to upload

FormData().append() doc at MDN.


So your code should be like this:

$("#upload-file-form").submit(function(e){
    e.preventDefault();  // <--------stops the form submission
    var fd = new FormData($("#upload-file-form")[0]);
        fd.append('groupName', 'xxx');

    $.ajax({
      url: "/uploadFile",
      type: "POST",
      data: fd,
      enctype: 'multipart/form-data',
      processData: false,
      contentType: false,
      cache: false,
      success: function(data) {
          if (data == "success") {
            $(".modal-body").html("<p>File uploaded Successfully</p>");
            $('#myModal').modal('show');
          } else if (data == "failure") {
            alert("failure)
          }
      }
    });
});

jQuery.ajax() to upload files example @ MDN

Sign up to request clarification or add additional context in comments.

5 Comments

Ya I tried this , this is the error I am getting "There was an unexpected error (type=Method Not Allowed, status=405). Request method 'GET' not supported ". But my request is POST type ..
Most probably your form is getting submitted you need to stop the form submission to navigate to the action path.
you mean event.preventDefault() method?
Ya I tired that ."This is the error I am getting 400 (Bad Request)". I tried by converting fd to Json String ,but it says again that it it is not a multipart request. Is there an error in the code for server side?
can you post your form in your question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.