I want to update the user with a name and/or image.
The name is always on the input because I fetched from the database but the fileUpload is optional.
How do I make my controller handle an optional parameter for the fileUpload?
Atm, if I don't upload something I'm getting
HTTP Status 400 - Required request part 'fileUpload' is not present
Spring controller
@RequestMapping(value = "/user/update", method = RequestMethod.POST)
public @ResponseBody String updateUser(HttpSession session,@RequestParam("name") String name, @RequestParam("fileUpload") MultipartFile image) {
String loggedUser = session.getAttribute("loggedUser").toString();
return User.updateUser(loggedUser,name,image);
}
Ajax
function updateUserSettings() {
var name = $('#userName').val();
var formData = new FormData();
formData.append("name", name);
formData.append("fileUpload", file);
$.ajax({
type: 'post',
url: '/user/update',
data: formData,
enctype: 'multipart/form-data',
processData: false,
contentType: false
});
}