Commit 8e590100 authored by Matthieu BENOIST's avatar Matthieu BENOIST
Browse files

Merge branch 'DOS0186370' into 'master'

Corrects some pdf download problems with neogeo services

See merge request !162
parents 935ecf03 aba2b1ea
Pipeline #25006 passed with stage
in 10 minutes and 15 seconds
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;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment