I'm trying to upload a file with some other form data from AngularJS using $http to Spring MVC controller. I get this error:
HTTP Status 400 - Required request part 'file' is not present
The request sent by the client was syntactically incorrect.
Here is the AngularJS
code sending the post
request:
service.create = function(id, photo) {
var formData = new FormData();
formData.append('name', photo.name);
formData.append('file', photo.file);
formData.append('story', photo.story);
if (!photo.url) {
photo.url = null;
}
formData.append('url', photo.url);
return $http(
{
method : 'POST',
transformRequest: angular.identity,
transformResponse: angular.identity,
url : BASE_URL + authService.getToken().id
+ '/resource/' + id + '/photo/',
data : formData,
headers : {
'Content-Type' : undefined
}
}).then(function(res) {
return res;
});
};
And here is my Spring MVC controller
method receiving the request:
@Override
@RequestMapping(path = "user/{uid}/resource/{lid}/photo", method = RequestMethod.POST)
public Photo create(HttpServletRequest request, HttpServletResponse response, @PathVariable int uid, @PathVariable int lid,
@RequestParam MultipartFile file, @RequestParam String name, @RequestParam String url, @RequestParam String story) {
return photoDAO.create(uid, lid, file, name, url, story);
}
I also have the MultipartResolver
bean added to my config:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="10000000" />
</bean>
What could be causing the controller not to find the file
request parameter? How else should I be reading the file to get it as a MultipartFile
?
Note: I am aware there are many other questions similar to mine in one way or another. None of the answers there helped me.
the file is being sent as part of the payload. This is a how the payload looks like:
------WebKitFormBoundaryK0onA6CrI1KaKcJY Content-Disposition: form-data; name="name"
n
------WebKitFormBoundaryK0onA6CrI1KaKcJY Content-Disposition: form-data; name="file"
PNG [REMOVED FOR SPACE] % V4¡´ºÝÝ6nI6Ú"èdöîÎÉÎ83»ý¡OEP|1êÄ¿· (õÛ>´/
------WebKitFormBoundaryK0onA6CrI1KaKcJY Content-Disposition: form-data; name="story"
st
------WebKitFormBoundaryK0onA6CrI1KaKcJY Content-Disposition: form-data; name="url"
null
------WebKitFormBoundaryK0onA6CrI1KaKcJY--