【发布时间】:2020-10-09 13:38:08
【问题描述】:
我正在尝试在 Angular 9 的新选项卡中打开 pdf 文件。我从 api 接收文件作为 blob。但是由于 window.URL.createObjectURL(blob); 已被弃用,我收到了这个 Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided. 错误。我看到现在我应该使用MediaStream() 进行他的操作,但我不知道如何使它与 blob 一起使用。
我的代码现在看起来像这样,但它缺少主要部分:
downloadFile() {
console.log('File download started.');
const blob = this.agreementService.getPdfReport(this.agreementNumber);
// Deprecated part
const url = window.URL.createObjectURL(blob);
const link = this.downloadZipLink.nativeElement;
link.href = url;
link.download = 'Agreement.pdf';
link.click();
window.URL.revokeObjectURL(url);
}
agreement.service.ts:
getPdfReport(agreementNumber: string){
this.requestUrl = `${configs.api}v1/reports/${agreementNumber}/Agreement.pdf`;
let headers = new HttpHeaders();
headers = headers.set('Accept', 'application/pdf');
return this.http.get(this.requestUrl, {headers, responseType: 'blob'});
}
【问题讨论】:
标签: angular typescript