I want to have an input field with file. Put the file into an Angular object, then upload it into API.
But, when I log file, it's always empty (like {}
). The rest API receive empty object too. Server-side result: {"id":0,"name":"test","image":{}}
.
My object:
export interface Image {
id: number;
name: string;
image: Blob;
}
In my components, the HTML is <input type="file" name="file" id="file" (change)="handleFileInput($event.target)" required>
, and my typescript code is:
public imageEdit: Image = { id: 0, name: "something" };
public handleFileInput(file: any): void {
let files = file.files as FileList;
if(files.length == 1) {
console.log(JSON.stringify(files));
this.imageEdit.image = files.item(0); // also tried with "files[0]"
this.http.post("my/url", this.imageEdit); // here send to server
}
}
I also tried to use Uint8Array
instead of Blob
.