Skip to content

Instantly share code, notes, and snippets.

@bverbeken
Last active July 22, 2018 13:37
Show Gist options
  • Select an option

  • Save bverbeken/8282925 to your computer and use it in GitHub Desktop.

Select an option

Save bverbeken/8282925 to your computer and use it in GitHub Desktop.
angular, spring MVC and multipart/form-data
$scope.saveMyObject = function () {
return $http({
method: 'POST',
url: '/my/url',
headers: {
'Content-Type': undefined
},
data: {
file: $scope.file,
startDate: $scope.startDate,
endDate: $scope.endDate
},
transformRequest: function(data) {
var fd = new FormData();
angular.forEach(data, function(value, key) {
fd.append(key, value);
});
return fd;
}
});
};
...
@RequestMapping(method = POST, consumes = "multipart/form-data")
public void submit(@RequestBody MultiValueMap<String, Object> requestBody) {
// do stuff
}
@andypetrella
Copy link

Good luck ^^

@gcuisinier
Copy link

I'am not sure that @RequestBody and MessageConverter is needed.

You can try with

RequestMapping(method = POST, consumes = "multipart/form-data")
public void submit(@RequestParam("startDate") String startDate,
@RequestParam("startDate") String endDate,
@RequestParam("file") MultipartFile file) {

But that need you to add some configuration to handle MultipartFile :

<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment