在Angular 7应用程序中,如何进行get API调用并将响应下载为pdf文件?

时间:2019-08-22 18:59:10

标签: api download angular7 response

我正在尝试进行get API调用,一旦响应返回,应将其下载为PDF文件。

我有可以进行api调用的服务

    // printable pdf for confirmation page
    getPdfConfirmationView(payload) {
      return this.http.get(environment.apiUrl + 
      '/report/getPdfConfirmationView', { headers: 
      this.getSearchApiHeaders(payload), responseType: 'blob' });
    }

在我的组件中,我使用以下方法进行api调用:

      getPdf() {
        const payload = {applicantId: this.applicantIdHeader, commentCodes: 
        this.commentCodeHeader, genderType: this.gender, data: this.data }
        this.studentService.getPdfConfirmationView(payload).subscribe(
          (pdfResponse: any) => {
          console.log('PDF RESPONSE: ', pdfResponse)
          const blob = new Blob([pdfResponse], { type: 'application/pdf' });

          const url = URL.createObjectURL(blob);
          window.open(url);
         })
       }

当我在控制台上记录我的响应(pdfResponse)时,我会在浏览器中得到它:

PDF RESPONSE:  Blob {size: 0, type: "text/xml"}

我了解window.open()在新选项卡中打开url,那不是我想要的,而不是在其他浏览器中打开,应将响应下载为pdf。

0 个答案:

没有答案
相关问题