通过REST Api下载CSV

时间:2019-09-20 13:44:05

标签: angular typescript rest csv

我必须从REST Api下载CSV。 我正在通过服务通过Http获取请求

user.service.ts

  public downloadCsv(target: string): Observable<any> {
    return this.http.get<any>(
      this.api.getUrl(environment.api.endpoints.download).replace('{target}', target)
    ).pipe(
      catchError(this.api.handleError)
    );
  }

并在我的组件中订阅可观察的

customer-list.component.ts

  downloadCsv() {
    this.userService.downloadCsv('users_ub').subscribe(
      (response) => {

      }
    );
  }

我遇到错误

VM73806 vendor.js:56089 ERROR Error Code: 200
Message: Http failure during parsing for http://localhost:90/api/download/csv/users_ub.csv

在Chrome开发工具中请求数据(角度)

从我的api文档中请求数据

enter image description here

1 个答案:

答案 0 :(得分:2)

使用{ responseType: 'blob' }

this.http.get<any>(url,{ responseType: 'blob' })