Skip to content
Snippets Groups Projects
Commit aba2b1ea authored by Matthieu Benoist's avatar Matthieu Benoist
Browse files

corrects some pdf download problems with neogeo services

parent 935ecf03
Branches
Tags
1 merge request!162Corrects some pdf download problems with neogeo services
import { HttpClient, HttpResponse } from '@angular/common/http'; 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 { Component, Input, OnDestroy, OnInit, OnChanges } from '@angular/core';
import { Subject } from 'rxjs'; import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators'; import { takeUntil } from 'rxjs/operators';
...@@ -40,16 +41,49 @@ export class DownloadButtonComponent implements OnInit, OnDestroy, OnChanges { ...@@ -40,16 +41,49 @@ export class DownloadButtonComponent implements OnInit, OnDestroy, OnChanges {
takeUntil(this.ngUnsubscribe), takeUntil(this.ngUnsubscribe),
).subscribe( ).subscribe(
(response) => { (response) => {
// Create a temporary link and click on it to launch the blob download
const url = window.URL.createObjectURL(response.body); const url = window.URL.createObjectURL(response.body);
const a = document.createElement('a');
a.href = url; if (response.body.type == 'application/pdf') {
a.rel = 'noopener'; var reader = new FileReader();
a.download = this.fileName; reader.readAsDataURL(response.body);
document.body.appendChild(a); // append the element to the dom -> otherwise it will not work in firefox reader.onloadend = () => {
a.click(); const bytes = reader.result.toString();
a.remove(); //what we realy have ?
this.loading = false; 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) => { (err) => {
let message = notificationMessages.general.failedDownloadFile; let message = notificationMessages.general.failedDownloadFile;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment