Angular 4属性“ body”在“ Response”类型上不存在

时间:2018-07-20 13:37:49

标签: angular

当我尝试从Web api调用中获取csv时,出现以下错误。当不带角度地访问该URL时,它将下载csv。

Property 'body' does not exist on type 'Response'.

服务

getViolationFile(mid: string, cp: string, d: string, pl: string) {
    let params: URLSearchParams = new URLSearchParams();
    params.set('mid', mid);
    params.set('cp', cp);
    params.set('d', d);
    params.set('pl', pl);

    let headers = new Headers({
      'Accept': 'text/csv'
    });

    let options = new RequestOptions({
      headers: headers,
      search: params,
    });

    return this.http.get('MappViolations/Download?', options);
  }

组件

public submit() {
    this.incomingMappSummaryService.getViolationFile(this.mid, this.cp, this.domain, this.pl)
      .subscribe(data => this.downloadFile(data.body), //gets error here 
      error => console.log('error'),
      () => console.log('success'));
  }



downloadFile(data: Response) {
    let contentType = 'text/csv';
    let blob = new Blob([data.body], { type: contentType }); //this might not be right either but I dont get an error for it
    saveAs(blob, 'data.csv');
    let url = window.URL.createObjectURL(blob);
    window.open(url);
  }

3 个答案:

答案 0 :(得分:1)

在您的服务上尝试一下:

return this.http.post(endPointApi, body, options)
.map((data) => { return data.text(); })
.catch(error => { console.log(error)});

答案 1 :(得分:0)

尝试使用括号表示法:data ['body']

否则,请再次检查您是否收到JSON格式的正确响应:

data.json()

答案 2 :(得分:0)

属性数据的类型已经是Response,因此无需使用data.body,只需:

this.downloadFile(data)