Reputation: 565
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.
Upvotes: 1
Views: 764
Reputation: 462
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?
Upvotes: 2