0

所以我尝试使用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>
4

2 回答 2

0

根据github 上讨论,我认为您需要从该方法返回一个对象。
尝试这个:

this.uploader.onBuildItemForm = (fileItem: any, form: any) => {
    form.append('imgs', fileItem);
    return {fileItem, form};
}; 
于 2019-03-15T16:14:56.057 回答
0

所以,在努力寻找解决方案之后,我终于明白了,在上传器中我不得不添加itemAlias

uploader = new FileUploader({
        url: this.FULL_URL,
        authToken: this.token`,
        itemAlias: 'imgFile'
    });
于 2019-03-18T12:54:37.747 回答