I have a function which is used for exporting files to excel. It detects when file is ready and then downloads it. This is how it looks:
private exportData() {
this.isExporting = true;
let streams = this.series.filter(s => !!s.streamId && !s.value);
let downloadToken = new Date().getTime() + '_' + (Math.round(Math.random() * 100000000)).toString();
let url = this.service.url + '/export/';
// Create invisible iframe which will navigate to request url.
// Content-Type will be application/octet-stream, which will cause
// the browser to download instead of displaying it.
let element = document.createElement('iframe');
element.style.visibility = 'hidden';
document.body.appendChild(element);
element.src = url;
// Detecting the beginning of a download is absolutely not straight
// Tried a couple of things, but all event listeners fire when the iframe is created.
// Ended up with this approach:
// https://stackoverflow.com/questions/1106377/detect-when-browser-receives-file-download
let interval = Observable.interval(500);
let subscription = interval.subscribe((value: number) => {
if (Cookie.Get('download_token') == downloadToken) {
Cookie.Expire('download_token');
this.isExporting = false;
this.exportModal.close();
subscription.unsubscribe();
}
}, (error: any) => {
this.service.HandleUiError(error);
this.isExporting = false;
});
}
However, when file is big it sometimes takes long time to create that in the backend, and I get 504 Gateway Time-out
error, but it is not caught in
this.service.HandleUiError(error);
this.isExporting = false; });
does anyone know why this could happen and how to fix it?