I am using a recent version of ANGULAR and all I want to achieve is to close the window when the download is finished.
It is supposed to work like this: when the user clicks on a link it opens this window that makes the download start and then it closes when download is finished, so that the close of the window doesn't stop the download.
I got this all sorted out for the main browsers except IE11, but I really need this to work on IE11.
This is what I have:
startDownload(filename: string, filecontent: string) {
let blob = DownloadUtils.b64toBlob(filecontent, "application/pdf");
let blobUrl = window.URL.createObjectURL(blob);
var downloadID = (new Date()).getTime();
var a = document.createElement('a');
a.setAttribute('href', blobUrl + "#downloadID=" + downloadID);
a.setAttribute('download', filename);
this.cookiePattern = new RegExp(("downloadID=" + downloadID), "i");
this.checkCookies();
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(blobUrl);
}
checkCookies() {
if (document.cookie.search(this.cookiePattern) >= 0) {
open('', '_self').close(); // download complete -> close window
return;
}
setTimeout(this.checkCookies, 500);
}
I was inspired by: https://laracasts.com/discuss/channels/javascript/using-javascript-to-check-if-a-file-has-been-downloaded
The problem with IE11 is that it doesn´t allow the download of blob URLs, I guess.
IE11 forces the use of:
window.navigator.msSaveBlob(blob, filename);
But with this I cant use the queryString "downloadID" to detect the end of download.
I also tried FileSaver.js but didn´t work.
I also tried to set:
a.setAttribute('href', 'data:application/pdf;base64,' + filecontent + "#downloadID=" + downloadID);
Which works, except on IE11 again. I get this error:
"The data area passed to a system call is too small."
NOTE: The problem isn't closing the page, but knowing when to close.
I could use a timeout for IE11, but I rather not.
Can you please help?