I wanna do a file upload using angularjs as the front-end, and Spring Rest as the back-end. Here is my front-end code:
var data = evt.target.result;
var formData = new FormData();
formData.set("fileId", 1);
formData.set("file", data);
$http({method: "POST", url: "/file", data: formData, headers : {"Content-Type": undefined}, transformRequest: angular.identity}).success(function() {
console.log("hi");
}).error(function(reason) {
console.log(reason);
});
And here is my back-end code:
@RequestMapping(value = "/file", method = POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<Void> uploadFile(@RequestPart("file") MultipartFile file) {
I think my code should be working, but in fact, I keep getting error 400 saying that "Required request part 'file' is not present".
any ideas? Thanks.
Here is the data from the firebug:
-----------------------------5045635351933894389797998578
Content-Disposition: form-data; name="fileId"
1
-----------------------------5045635351933894389797998578
Content-Disposition: form-data; name="file"
%PDF-1.7
%äãÃÃ’
4 0 obj
<</Type/XObject
/Subtype/Form
/FormType 1
/Matrix[1 0 0 1 0 0]
/BBox[0 0 612 792]
/Resources<</ExtGState<</GS0 5 0 R>>/ProcSet[/PDF/ImageC]/Properties<</MC0 6 0 R>>/XObject<</Im0 7 0
R>>>>/Filter/FlateDecode/Length 343>>
stream
H‰”“ÃNÃ0†ïy
¿@SDZ“øÊ†&;ð
-----------------------------5045635351933894389797998578--
We can clearly see the param name, one is fileId and the other is file.