2

I am trying to download ICS file in all the browsers. I am able to download ICS file in chrome but not in safari. Here is the javascript code that i used.

if(isChrome)
{
            var blob=new Blob([data]);
            var link=document.createElement('a');
            link.href=window.URL.createObjectURL(blob);
            link.download=fileName+".ics";
            link.click()
}
if(!isChrome) 
{
`   alert("not chrome");
    var downloadLink = document.createElement('a');
    downloadLink.setAttribute('href', 'data:application/octet;charset=utf-8,' + escape(data));
    var fileName = fileName+'.ics';
    downloadLink.setAttribute('download', fileName);
    var clk = document.createEvent("MouseEvent");
    clk.initEvent("click", true, true);
    downloadLink.dispatchEvent(clk);
    document.body.appendChild(downloadLink);
    downloadLink.dispatchEvent(clk);
    //download(data, fileName+".ics", "text/calendar");
}

In Safari browser the generated file is getting downloaded but that file name is not getting appended. The filename i am getting is "Unknown" but the size of the file is 3KB and the content is there only the filename and extension is not getting added to the file.

Please let me know how i can solve this issue.

Thanks in advance

Venkat
  • 2,549
  • 2
  • 28
  • 61
Akhila
  • 21
  • 1
  • 3

2 Answers2

1

You should specify blob type. { type: 'text/calendar' }

        const blob = new Blob([data], { type: 'text/calendar' });
        const link = document.createElement('a');
        link.href = window.URL.createObjectURL(blob);
        link.download = filename + ".ics" ;
        link.click();

Use this to download the file successfully, but you have to manually add it to the calendar. Safari does not include ics in a list of safe files and it will not be opened automatically. enter image description here

IvoAtanasov
  • 166
  • 2
  • 12
0

If the problem is that you're using safari 10.1 or below the problem might be that it doesn't like the download attr. http://caniuse.com/#feat=download