首页 > 解决方案 > 在Angular 9中进行post调用以将文件传递给express-fileupload的正确方法是什么?

问题描述

我可以像那样在邮递员中做到这一点 邮递员电话工作完美

但在 Angular 中不起作用,这是 Angular 代码 js:

<input type="file" #fileUpload id="fileUpload" name="file" multiple="multiple" accept=".csv"  />
const formData = new FormData();
    formData.append('file', file);

    this.caricautentiService.caricautenti(this.globals.idPersona,formData).subscribe( response => { 
        console.log(response);
    });

这是服务:

    if (file !== undefined) {
        formParams = formParams.append('file', <any>file) as any || formParams;

    }

    return this.httpClient.request<any>('post',`${this.basePath}/caricautenti/${encodeURIComponent(String(idpersona))}`,
        {
            body: formParams,
            withCredentials: this.configuration.withCredentials,
            headers: headers,
            observe: observe,
            reportProgress: reportProgress
        }
    );

我错过了什么?任何帮助表示赞赏,谢谢

标签: node.jsangularexpresspostman

解决方案


这对我有用:

public addFileToProgram(file: any): Observable<any> {
    const formData = new FormData();
    formData.append('file', file, file.name);
    return this.http.post<any>('/v1/uploadfile', formData);
}

推荐阅读