所以我尝试使用angular 7 / ng2-file-upload 上传文件,但我无法这样做,
因为我收到错误 400(需要字段名称),问题是我找不到添加方法(键名/字段名)到文件。
这是 Post-man 的一个示例:
我使用 Form-data,然后添加一个键名(imgfile),然后在值字段中浏览到文件,当我单击上传时,它就像一个魅力。但是,例如,如果我将键名更改为 ex: (imgs) 我复制了我在角度 7 中得到的错误。
这是我的 TS:
uploader = new FileUploader({
url: this.FULL_URL
});
ngOnInit() {
const authHeader: Array<{
name: string;
value: string;
}> = [];
authHeader.push({
name: 'Authorization',
value: `Bearer ${this.token}`
});
const uploadOptions = <FileUploaderOptions>{ headers: authHeader };
this.uploader.setOptions(uploadOptions);
this.uploader.onBuildItemForm = (fileItem: any, form: any) => {
form.append('imgfile', fileItem);
//here is where i think i should added the key name,,, but i could not
manage to make it work
};
}
这是我的html:
<input type="file" ng2FileSelect [uploader]="uploader">
<button (click)="uploader.uploadAll()">Upload </button>