Actually, I'm working on a Spring REST API with an interface coded in Angular 2.
My problem is I can't upload a file with Angular 2.
My Webresources in java is that :
@RequestMapping(method = RequestMethod.POST, value = "/upload") public String handleFileUpload(@RequestParam MultipartFile file) { //Dosomething }
And it is perfectly working when I call it through URL request with Auth header etc ... ( with Advanced Rest Client extension for Chrome )
Proof: (everything works fine in that case )
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
Spring config file and the Pom dependency
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2</version> </dependency>
BUT when I try to do the same thing with a webform :
<input type="file" #files (change)="change(files)"/> <pre>{{fileContents$|async}}</pre>
With the (change) method :
change(file) { let formData = new FormData(); formData.append("file", file); console.log(formData); let headers = new Headers({ 'Authorization': 'Bearer ' + this.token, 'Content-Type': 'multipart/form-data' }); this.http.post(this.url, formData, {headers}).map(res => res.json()).subscribe((data) => console.log(data)); /* Observable.fromPromise(fetch(this.url, {method: 'post', body: formData}, {headers: this.headers} )).subscribe(()=>console.log('done')); */ }
My web service returns me an error 500, with that in tomcat logs: http://pastebin.com/PGdcFUQb
I tried the 'Content-Type': undefined
method too but without success ( the web service return me a 415 error in that case.
Can someone help me to figure out what's the problem is?
Problem solved, I'll update that question later with my code :) but, have a look on the plunker it's working perfectly well. Thanks.