The current request is not a multipart request

20,027

You are sending an Ajax request and you are using name of input field directly in your controller which cause the problem. Because when ajax request come to the controller it doesn't found any parameter with name "uploadfile" that's why it giving you error.

Here I've just put key for the file and it accept request. The code written below is work for me.

Ajax code,

var formData = new FormData();
var file = $('#fileInputId')[0].files[0];

formData.append("myFileKey", file);

$.ajax({
        url : 'myUrl',
        type : 'POST',
        data : formData,
        enctype : 'multipart/form-data',
        contentType : false,
        cache : false,
        processData : false,
        success : function(response) {},
        error: function(){}    
     )};

Java Controller Code:,

    @PostMapping(value = { "/myUrl" }, consumes = { "multipart/form-data" })
    public ModelAndView readFileData(@RequestParam("myFileKey") MultipartFile uploadedFile) throws IOException 
   {
                 Your Stuff...... 
   }
Share:
20,027
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    On uploading my file by ajax I am facing Exeption

    org.springframework.web.multipart.MultipartException: The current request is not a multipart request

    I google this and find many solution,applied all of them no one resolved my problem-

    Below is my html-file

    <form id="upload-file-form">
       <label for="upload-file-input">Upload your file:</label>
       <input id="upload-file-input" type="file" name="uploadfile" accept="*" enctype="multipart/form-data" />
    </form>
    

    Script for ajax-

    $.ajax({
             url: "/util/uploadFile",
             type: "POST",
             data: {'uploadfile':new FormData($("#upload-file-form")[0])},
             enctype: 'multipart/form-data',
             processData: false,
             contentType: false,
             cache: false,
             success: function () {},
             error: function () {}
             });
    

    And this is my Spring boot Controller("/util")-

    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
        @ResponseBody
        public String uploadFile(@RequestParam("uploadfile") MultipartFile uploadfile) {
            System.out.println("----------------");
            System.out.println("----------------" + uploadfile);
            return "success";
        }
    
    
        @Bean
        public MultipartConfigElement multipartConfigElement() {
            return new MultipartConfigElement("");
        }
    
        @Bean
        public MultipartResolver multipartResolver() {
            org.springframework.web.multipart.commons.CommonsMultipartResolver multipartResolver = new org.springframework.web.multipart.commons.CommonsMultipartResolver();
            multipartResolver.setMaxUploadSize(4000000);
            return multipartResolver;
        }
    
    • Blank
      Blank almost 8 years
      Try to add enctype="multipart/form-data" in form tag.