create FormData () and add json and file
if (form.validate()) { var file = $scope.file; var fd = new FormData(); fd.append('jsondata', $scope.jsonData); fd.append('file', file); MyService.submitFormWithFile('doc/store.html', fd, '', (response){ console.log(response) }); }
// Service called above
MyService.submitFormWithFile = function(url, data, config, callback) { $http({ method : 'POST', url : url, headers : { 'Content-Type' : undefined }, data : data, transformRequest : function(data, headersGetterFunction) { return data; } }).success(function(response, status, header, config) { if (status === 200) { callback(response); } else { console.log("error") } }).error(function(response, status, header, config) { console.log(response); }); };
// in your part of Java using ObjectMapper
//it is like string fd.append('jsondata', JSON.stringify($scope.jsonData)); @Autowired private ObjectMapper mapper; @RequestMapping(value = "/companies", method = RequestMethod.POST) public void createCompany(@RequestParam String jsondata, @RequestParam(required = true) MultipartFile file){ CompanyDto companyDto=mapper.readValue(jsondata, CompanyDTO.class); ...... }
source share