28
loading...
This website collects cookies to deliver better user experience
%PDF-1.5
%âãÏÓ
1 0 obj
/Type/Page/Parent 8 0 R /MediaBox[ 0 0 612 792]/Contents 9 0 R /Resources/XObject/img49352 7 0 R /img49350 6 0 R /img49348 5 0 R /img49347 3 0 R /Font/F2 2 0 R /F4 4 0 R
endobj.................
data:application/pdf;base64,JVBERi0xLjUKJeLjz9MKMSAwI.....
private processFileResponse(fileResponseData: any, fileName: string, render: string): void {
if (render === 'base64') {
this.base64Response = fileResponseData;
const binaryString = window.atob(fileResponseData);
const bytes = new Uint8Array(binaryString.length);
const binaryToBlob = bytes.map((byte, i) => binaryString.charCodeAt(i));
const blob = new Blob([binaryToBlob], { type: 'application/pdf' });
this.downloadFile(blob, fileName, render);
} else {
const blob = new Blob([fileResponseData], { type: 'application/pdf' });
this.downloadFile(blob, fileName, render);
}
}
createObjectURL()
href
attribute private downloadFile(blob: any, fileName: string): void {
// IE Browser
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, fileName);
return;
}
// Other Browsers
const url = (window.URL || window.webkitURL).createObjectURL(blob);
const link = this.renderer.createElement('a');
this.renderer.setAttribute(link, 'download', fileName);
this.renderer.setAttribute(link, 'href', url);
this.renderer.setAttribute(link, 'target', '_blank');
this.renderer.appendChild(this.elementRef.nativeElement, link);
link.click();
this.renderer.removeChild(this.elementRef.nativeElement, link);
setTimeout(() => {
window.URL.revokeObjectURL(url);
}, 1000);
}
Once the process is completed, make sure you have revoked the object URL else that will pile up on the browser's memory
private downloadFile(blob: any, fileName: string): void {
// IE Browser
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, fileName);
return;
}
// Other Browsers
const url = (window.URL || window.webkitURL).createObjectURL(blob);
window.open(url, '_blank');
// rewoke URL after 15 minutes
setTimeout(() => {
window.URL.revokeObjectURL(url);
}, 15 * 60 * 1000);
}
import { saveAs } from 'file-saver-es';
...
private downloadFile(blob: any, fileName: string): void {
...
if (this.useFileSaverAPI()) {
saveAs(blob, fileName);
} else {
// rest of devices and browsers
window.open(url, '_blank');
}
}
private useFileSaverAPI(): boolean {
return (/(iPad|iPhone|iPod)/g.test(navigator.platform || navigator.userAgent) || ((navigator.platform || navigator.userAgent) === 'MacIntel' && navigator.maxTouchPoints > 1)) && !window.MSStream;
}
if (this.isAndroid()) {
if (this.base64Response) {
window.open(this.base64Response, '_blank');
} else {
const fileReader = new FileReader();
fileReader.readAsDataURL(blob);
fileReader.onloadend = () => {
window.open(fileReader.result as any, '_blank');
};
}
} else {
// rest of devices and browsers
window.open(url, '_blank');
}
private isAndroid(): boolean {
return (/(android)/i.test(navigator.platform || navigator.userAgent)
}