如何以角度发布文件对象?

时间:2018-10-30 14:44:42

标签: angular http

我将file称为File。如何在file中传递url

 importData(repoId: string, file: File ): Observable<any> {
    console.log(file);
    let url = this.config.getConfig("serviceEndpoints").importRepostory;
    url = this.formatString(url, { repoid: repoId, file: file });
    console.log(url);

    return this.http.post(url, null)
  }

1 个答案:

答案 0 :(得分:0)

您可以这样做:

const fd = new FormData();
fd.append('file', file);

const req = new HttpRequest('POST', 'TARGET_API_URL', fd, {
  reportProgress: true
});

this._http.request(req).pipe(...).subscribe(...); // Inject HTTP client in constructor.

有关详细实现:https://github.com/wannadream/angular-material-file-upload/blob/master/src/app/material-file-upload/material-file-upload.component.ts