diff --git a/src/app/shared/components/download-button/download-button.component.ts b/src/app/shared/components/download-button/download-button.component.ts index 0094a1f67e895ee6049bd268a5baca86f9abfec7..483584f1686b076dd94afdfbaf8aa1a741acefef 100644 --- a/src/app/shared/components/download-button/download-button.component.ts +++ b/src/app/shared/components/download-button/download-button.component.ts @@ -1,4 +1,5 @@ import { HttpClient, HttpResponse } from '@angular/common/http'; +import { toBase64String } from '@angular/compiler/src/output/source_map'; import { Component, Input, OnDestroy, OnInit, OnChanges } from '@angular/core'; import { Subject } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; @@ -40,16 +41,49 @@ export class DownloadButtonComponent implements OnInit, OnDestroy, OnChanges { takeUntil(this.ngUnsubscribe), ).subscribe( (response) => { - // Create a temporary link and click on it to launch the blob download const url = window.URL.createObjectURL(response.body); - const a = document.createElement('a'); - a.href = url; - a.rel = 'noopener'; - a.download = this.fileName; - document.body.appendChild(a); // append the element to the dom -> otherwise it will not work in firefox - a.click(); - a.remove(); - this.loading = false; + + if (response.body.type == 'application/pdf') { + var reader = new FileReader(); + reader.readAsDataURL(response.body); + reader.onloadend = () => { + const bytes = reader.result.toString(); + //what we realy have ? + const header = bytes.substring(0,bytes.indexOf(',') +1); + var str = bytes.substring(bytes.indexOf(',') + 1); + + const b64 = atob(str).replace('"',''); + + var newData = ''; + try { + atob(b64.replace('"','')); + newData = header + b64.replace('"',''); + } catch (e) { + newData = bytes; + } + finally { + + const a = document.createElement('a'); + a.href = newData; + a.rel = 'noopener'; + a.download = this.fileName; + document.body.appendChild(a); // append the element to the dom -> otherwise it will not work in firefox + a.click(); + a.remove(); + this.loading = false; + } + } + } + else { + const a = document.createElement('a'); + a.href = url; + a.rel = 'noopener'; + a.download = this.fileName; + document.body.appendChild(a); // append the element to the dom -> otherwise it will not work in firefox + a.click(); + a.remove(); + this.loading = false; + } }, (err) => { let message = notificationMessages.general.failedDownloadFile;