Javascript window.open() doesn’t download file

Tags: , ,



In Chrome window.open() downloads the ICS file, but in MS Edge, it’s trying to open the file in a different tab. How do I ensure Edge downloads the file in the same way Chrome does.

Code:

var icsMSG = ""; // Here goes the calendar details
window.open("data:text/calendar;charset=utf8," + escape(icsMSG))

Answer

Are you using Edge Legacy? Edge Legacy only support msSaveBlob method to download files. You can update your Edge to the latest Edge Chromium to make window.open() download or use msSaveBlob in Edge Legacy.

If you still want to use Edge Legacy, you can refer to the following code to use msSaveBlob to download:

    var icsMSG = ""; // Here goes the calendar details
    if (window.navigator && window.navigator.msSaveBlob) { // For IE & Edge Legacy
        var blob = new Blob([icsMSG], { type: 'text/calendar;charset=utf-8'' })
        window.navigator.msSaveBlob(blob, 'download.ics')
    }
    else {
        window.open("data:text/calendar;charset=utf8," + escape(icsMSG));
    }


Source: stackoverflow