мой код jquery выглядит следующим образом
var form = $ ('form #' + 'wizard2');// Сериализация всех входных значений (не файлов!) В коллекции Array, чтобы мы могли повторить эту коллекцию позже.var params = form.serializeArray ();
var files = $("#parafile");
//Declaring new Form Data Instance
var formData = new FormData();
//Looping through uploaded files collection in case there is a Multi File Upload. This also works for single i.e simply remove MULTIPLE attribute from file control in HTML.
for (var i = 0; i < files.length; i++) {
formData.append(files[i].name, files[i]);
}
//Now Looping the parameters for all form input fields and assigning them as Name Value pairs.
$(params).each(function (index, element) {
console.log(element.name +' =='+ element.value);
formData.append(element.name, element.value);
});
$.ajax({
url : '/paragliding/saveNewPackage',
type: "POST",
data : JSON.stringify(formData),
cache: false,
processData: false,
contentType: false,
enctype: 'multipart/form-data',
cache: false,
success : function(data) {
swal({
title: "Well done!",
text: "You successfully completed the form wizard.",
type: "success"
});
location.reload(true);
},
error: function (error) { alert(error); }
});
и мой код на стороне сервера имеет вид
@ RequestMapping (value = {"/ saveNewPackage"}, метод = RequestMethod.POST, потребляет= MediaType.MULTIPART_FORM_DATA_VALUE, заголовки = "Content-Type = multipart / form-data")
public String saveParagliding(Authentication authentication, HttpServletRequest request,
@RequestParam ("model") String model, @RequestParam (value = "parafile[]", required = false) MultipartFile[] file)
throws JsonParseException, JsonMappingException, IOException
{
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
boolean isAuthenticated = userDetails.getAuthorities().stream().anyMatch(grantedAuthority -> {
String grants = grantedAuthority.getAuthority();
return grants.equalsIgnoreCase(UserRole.ROLE_TYPE_ADMIN.getRoleName())
|| grants.equalsIgnoreCase(UserRole.ROLE_TYPE_CAMP_PARTNER.getRoleName());
});
if (!isAuthenticated)
{
return "UnAuthorized";
}
}