简体   繁体   English

Excel Export Angular 5获取请求

[英]Excel Export Angular 5 Get Requests

I have an endpoint I ping via a GET and it responds with an excel sheet that should be auto downloaded so the user can view. 我有一个端点,我通过GET进行ping操作,并以自动下载的excel表格作为响应,以便用户查看。

I am using angular 5 and am not sure how to handle the response of this api and trigger an auto download... 我正在使用angular 5,不确定如何处理此api的响应并触发自动下载...

I can view the response in my console via a blob in my console *right now im just console.logging the response from api 我可以通过控制台中的Blob在控制台中查看响应*现在仅是console.loging from api

console output 控制台输出

this is what my service function is looking like thus far ... 到目前为止,这就是我的服务功能。

getPurgeExeclReport(queryParam): Observable<any> {

  // everything regarding param contructed in Class

    this.searchUrl = `${this.searchUrl}${queryParam}`;

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

// in my component //在我的组件中

  this.purgeEligibleReportService.getPurgeExeclReport(queryParams)
   .subscribe(result => {

    console.log(result, 'result')

   });

Please help! 请帮忙!

try to give a try to this .. 尝试尝试一下..

before do a 之前做一个

npm i --save file-saver @types/file-saver

then in your ts: 然后在您的ts中:

import * as FileSaver from 'file-saver';

public async DownloadRicetta(id: string, name: string): Promise<Blob> {
    return new Promise<Blob>((resolve, reject) => {
      const headers = new HttpHeaders();
      headers.append('Accept', 'text/plain');

      this._http.get(environment.API_URL + `endpoint`, { headers: headers, responseType: "blob", observe: 'response' })
        .subscribe(response => {
          try {
            let blob = this.saveToFileSystem(response, name);
            resolve(blob);
          } catch (error) {
            reject(error);
          }
        });
    });

  }

  private saveToFileSystem(response, name: string): Blob {
    const contentDispositionHeader: string = response.headers.get('Content-Disposition');

    //for get original filename
    const parts: string[] = contentDispositionHeader.split(';');
    const filename = parts[1].trim().split('=')[1].trim();

    //get extension of the file
    const parts2: string[] = contentDispositionHeader.split('.');
    let ext = parts2[1];
    ext = ext.replace('"', '')
    //set mimetype

    const blob = new Blob([response.body], { type: 'YOURMIMETYPE'});
    FileSaver.saveAs(blob, name + '_ricetta.' + ext);

    return blob;
  }

Hope it helps you!! 希望对您有帮助!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM