正如@JBNizet所建议的那样,我实现了以下可观察对象:
零件:
getPDF(){ this.apiService.getPDF(this.invoiceId) .subscribe( (data: Blob) => { var file = new Blob([data], { type: 'application/pdf' }) var fileURL = URL.createObjectURL(file);// if you want to open PDF in new tab window.open(fileURL);var a = document.createElement('a'); a.href = fileURL;a.target = '_blank'; a.download = 'bill.pdf'; document.body.appendChild(a); a.click(); }, (error) => { console.log('getPDF error: ',error); } ); }服务:
getPDF(invoiceId : number): Observable<Blob> { this.url = this.main_url + '/invoices/generatepdf/'+invoiceId; var authorization = 'Bearer '+sessionStorage.getItem("access_token"); const headers = new HttpHeaders({ 'Content-Type': 'application/json', "Authorization": authorization, responseType : 'blob'}); return this.httpClient.get<Blob>(this.url, { headers : headers,responseType : 'blob' as 'json'}); }我引用的网址:
- 重载#2:https://angular.io/api/common/http/HttpClient#get
- 使用Http Post预览Blob:https: //dzone.com/articles/how-to-preview-blobs-with-http-post-and-angular-5



