I am currently developing an Angular 2 project, where I need to upload a file and send some parameters from the client to the server (Spring Rest Server). I have attempted to use the FormData Interface for this purpose. However, when I try to append a file (created from event.srcElement.files) to it and then log the object to the console, it displays an empty FormData object. On the server side, I am using @requestparam("file") to retrieve the file. Any assistance on this matter would be greatly appreciated.
Here is the code in my HTML file:
<input type="file" #uploadFile multiple="true" (change)="uploadFile($event)"/>
The component file looks like this:
uploadFile(event:any){
let file = event.target.files[0];
let fileName = file.name;
console.log(file)
console.log(fileName)
let formData = new FormData();
formData.append('file',file);
this.examService.uploadAnswer(formData);
}
And in the service file:
uploadAnswer(formData:FormData){
console.log(formData)
this.http.post(this.url+'/uploadanswer', formData)
.map((response: Response) => {
let res = response.json();
Object.keys(res).forEach(name =>
this.questions=res[name]
);
});