如何在typescript接口上设置文件对象为blob/字节数组?



我想有一个输入字段与文件。把这个文件放到一个Angular对象中,然后上传到API中。

但是,当我记录文件时,它总是空的(像{})。其余API也接收空对象。服务器端结果:{"id":0,"name":"test","image":{}}.

我对象:

export interface Image {
id: number;
name: string;
image: Blob;
}

在我的组件中,HTML是<input type="file" name="file" id="file" (change)="handleFileInput($event.target)" required>,我的打字代码是:

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
}
}

我也试着用Uint8Array代替Blob

它没有真正回答,但我改变了一些东西,现在它工作了。现在,我使用POST请求的完整体来发送它。

在服务器上,我使用context.bodyInputStream()来获取内容作为流(并将其作为blob放在数据库中)。

在客户端,与角,我使用http.post("my/url", event.target.files[0]).