0

I am trying to upload the file, it is working on my local system, but not working in the server.

<form class="form-group row"  style="height:100px;" id="uploading" method="post" enctype="multipart/form-data">
  <div class="col-md-10" align="center">
    <div class="form-group row" align="center">
      <label class="col-md-2 form-control-label"> File to upload:</label>
      <div class="col-md-10" >
        <div class="input-group">
          <input type="file" class="filestyle" data-buttonName="btn-primary" name="upload" id="upload" accept="*"/>
        </div>
      </div>
    </div>
    <div class="form-group row" id="buttonzone">
      <div class="col-sm-14">
        <div class="input-group">
          <button type="submit"  class="btn btn-success"    id="upload"   style="margin-left: 96px;">
          <i class="fa fa-cloud-upload"></i> Upload</button>
          <button type="button"  class="btn btn-danger"  id="cancel"  ><i class="fa fa-ban"></i> Cancel</button>
        </div>
      </div>
    </div>
  </div>
</form>


$("form#uploading").submit(function(){
  var formData = new FormData($(this)[0]);
  $.ajax({
    url : '/uploadController/upload',
    type: 'POST',
    data: formData,
    async: false,
    beforeSend: beforeSendHandler,
    success: function (data){
      var msg=data.msg;
      var obj=data.obj;
      if(data.success == true){
        $('#successmsg').html(msg);
        $('.alert-success').show();
        $('.alert-danger').hide();
        setTimeout(function(){
          $(".alert-success").alert('close');
        }, 10000);
      }else{
        $('#errmsg').html(msg);
        $('.alert-danger').show();
        $('.alert-success').hide();
        setTimeout(function(){
          $(".alert-danger").alert('close');
        }, 10000);
      }
    },
    cache: false,
    contentType: false,
    processData: false
  });
  return false;
});

Java code:

@RequestMapping(value = "/uploadController/upload",headers=("content-type=multipart/*"), method = RequestMethod.POST)
public @ResponseBody StatusResponse totxnsUpload(@RequestParam("upload") MultipartFile upload, HttpServletRequest request, HttpServletResponse response) throws IOException, NoSuchFieldException, SecurityException{
  logger.debug(" file upload controller");
  //my logic here
  }

I am getting this in browser console:

{
 "timestamp":1495781126083,
 "status":400,
 "error":"Bad Request",
 "exception":"org.springframework.web.bind.MissingServletRequestParameterException",
 "message":"Required MultipartFile parameter 'upload' is not present",
 "path":"/uploadController/upload"
}

But it is working on out of server, I don't what is the problem.

Durga
  • 545
  • 7
  • 21
  • 39

1 Answers1

1

the parameter "upload" as seen in @RequestParam("upload") MultipartFile upload is a required parameter. If it is working in some systems it means that it is getting a parameter named "upload". In your case it fails because it is not present in the request.

You do have an input named upload in your form though. But I can see you are trying to send form data using ajax. Can you see the request in browser dev tools network tab?

Also place a breakpoint in your totxnsUpload method and see if you are getting two form submit requests (one standard and one with ajax)

for debugging purposes you can set upload parameter to optional in your Java code with this replacement @RequestParam(value = "upload", required = false) MultipartFile upload

With that being said. If the exact same code is working on your machine but not working on the server, you might need to configure your context. Take a look at this How to use HttpServletRequest#getParts() in a servlet filter running on Tomcat?

masadwin
  • 422
  • 2
  • 5
  • 11
  • i tried your code but i am getting `{"timestamp":1495787848177,"status":500,"error":"Internal Server Error","exception":"java.lang.NullPointerException","message":"No message available","path":"/uploadController/upload"}` – Durga May 26 '17 at 08:46
  • yes this means that the request without `upload` parameter was accepted. but since the `upload` variable was null, it threw a nullpointer exception. I think the issue is probably in your ajax call. have you tried to inspect the request in your browser's dev tools->network tab? – masadwin May 26 '17 at 10:11
  • i am using csrf, that is the problem? – Durga May 26 '17 at 11:33
  • if I were to guess, that could be the case. can't hurt to try – masadwin May 26 '17 at 13:16
  • I got solution, your link is helped thanks a lot for your help – Durga May 29 '17 at 05:40
  • that is nice, Durga. Glad to be of help. – masadwin May 29 '17 at 07:02