0

I am using the spring mvc form,jquery to submit the form.Along with all the form values there is multipart file upload.

Following is the UI code,

<div class="form-group">
     <span class="fileinput-new">Select file</span> <span class="fileinput-exists">Change</span> 
     <form:input type="file" id="addrDocImage" path="addrDocImage" name="addrDocImage"/>
    </span> 
    </div>
 

here is my java script code

   $.ajax({
            url: form.action,
            type: form.method,
            data: $(form).serialize(),
            beforeSend: function(xhr){
             xhr.setRequestHeader('X-CSRF-Token', $("meta[name='_csrf']").attr("content"));
         },
            success: function(response) {
             $('#merchantAddressCreationForm').modal('hide');
             //$('#merchantAddressCreationForm').modal('toggle');
          swal({
                 title: "",
                 text: response,
             }, function() {
                $.ajax({
            type: 'GET',
            url: 'addressDetails',
            data : "_csrf="+$("meta[name='_csrf']").attr("content"),
            success:function(data){
             $(".merchant_address_data").html(data);
            },
            error:function(response){
            }
           });
       }); 
            }            
        });
    }
}); 

And my dto with multipart is

 public class AddressDTO implements Serializable {

private static final long serialVersionUID = 2931368070275666084L;
private long addrId;
private long partyId;
private String locationName;
private MultipartFile addrDocImage;
     ''''''
}

But I am getting the addrDocImage as null when I am submitting the form.

Following is the controller method

    @RequestMapping(value="/createMerchantAddress", method = RequestMethod.POST )
public @ResponseBody String createMerchantAddress(@ModelAttribute("addressDTO") AddressDTO addressDTO,@AuthenticationPrincipal PNSolUser loggedUser){
    addressDTO.setPartyId(loggedUser.getPartyId());
    addressDTO.setCreatedBy(loggedUser.getUserId());
    addressDTO.setCreatedDate(Calendar.getInstance().getTime());
Ramsad
  • 63
  • 1
  • 1
  • 11

0 Answers0