Durga
Durga

Reputation: 565

Required MultipartFile parameter 'file' is not present when uploading file

I am trying to upload file but i am getting Required MultipartFile parameter 'file' is not present 400 Bad Request org.springframework.web.bind.MissingServletRequestParameterException.

My code here:

<form id="uploadrecords" method="post" enctype="multipart/form-data">                     
 <div class="col-md-12">
 <div class="form-group" style="margin-top: 11px !important;">
 <label for="uploadfile" class="col-md-4 control-label"><span class="mandatory"></span>File to upload:</label>
 <div class="col-md-6">
 <input type="file" class="filestyle" data-buttonName="btn-primary" name="file" id="upload" accept="*"/>
 </div>
 </div>
 <div class="form-actions" style="margin-top: 51px;margin-left: 358px;">
 <button type="submit"  class="btn btn-success" style="padding: 6px 12px;"   id="upload" ><i class="fa fa-cloud-upload"></i> Upload</button>
 <button type="button"  class="btn btn-danger"  style="padding: 6px 12px;" id="cancel"  ><i class="fa fa-ban"></i> Cancel</button> 
 </div>
 </div>
</form>

Jquery code here:

$("form#uploadrecords").submit(function(){                                  
    var formData = new FormData($(this)[0]);                                    
    $.ajax({
    url : '/uploadfile', 
    type: 'POST',
    data: formData,
    async: false,
    beforeSend: beforeSendHandler,
    success: function (data) {
    var msg=data.msg;
    var obj=data.obj;   
    if(obj != 0){
    $("#countfail").html(obj);
    $("#download-modal").modal('show');
    }else{
    bootbox.alert(msg);
    }
    },
    cache: false,
    contentType: false,
    processData: false
    });
});

Java code:

@RequestMapping(value = "/uploadfile", headers = "Content-Type=multipart/form-data", method = RequestMethod.POST)
public @ResponseBody StatusResponse upload( @RequestParam(value = "file") MultipartFile file, HttpServletRequest request,
        HttpServletResponse response) throws IOException, NoSuchFieldException, SecurityException, ParseException {
    StatusResponse sr = new StatusResponse();
    System.out.println("filename::::" + file.getOriginalFilename());
    //my logic here
    return sr;
}

It is not hitting controller class. i find error in Firebug console.

I am using spring boot and jboss-eap-6.4.

What is wrong in my code. How to solve this problem?

Upvotes: 0

Views: 1084

Answers (1)

Masud
Masud

Reputation: 555

if you are using @EnableAutoConfiguration then you need to do the following as discussed here https://github.com/spring-projects/spring-boot/issues/2958

@EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class}) define the following beans

@Bean(name = "multipartResolver") public CommonsMultipartResolver commonsMultipartResolver(){ CommonsMultipartResolver resolver = new CommonsMultipartResolver(); resolver.setMaxUploadSize(50*1024*1024); return resolver ; }

@Bean @Order(0) public MultipartFilter multipartFilter(){ MultipartFilter multipartFilter = new MultipartFilter(); multipartFilter.setMultipartResolverBeanName("multipartResolver"); return multipartFilter; }

Upvotes: 1

Related Questions