Currently, I am working on implementing the functionality to open a PDF file in a new tab using Angular 9. The PDF file is received from an API as a blob. However, I have encountered an issue due to the deprecation of
window.URL.createObjectURL(blob);
. This has led to an error message stating Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided.
. After researching, I discovered that I should now use MediaStream()
for such operations, but I am struggling to understand how to make it work with a blob.
The current code snippet I have looks like this:
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'});
}