data:text/calendar not recognized by Internet Explorer

大兔子大兔子 提交于 2019-12-07 06:15:53

问题


I'm trying to create an ics file on the fly using only JavaScript without an intermediate save. I'm restricted to JavaScript and no save because of the platform this will eventually be running on.

I'm a novice JavaScript programmer, but I've cobbled together something that works (almost) by a lot of searching on the web. What I've come up with works in Chrome, Firefox, and even Opera, but it doesn't work in Internet Explorer. In all other browsers I get an ics file created and downloaded, but in Explorer I get "The webpage cannot be displayed" with the entire ics stream displayed in the address bar beginning with the following:

data:text/calendar;charset=utf8,BEGIN%3AVCALENDAR

Everything from "BEGIN" on is part of what should be part of the ics file.

The bit of code that successfully starts the download in all the other browsers is this:

 window.open("data:text/calendar;charset=utf8," + encodeURI(icsMSG));

icsMSG is the string that composes the actual ics file.

As mentioned the only issue is that this isn't working in IE as IE doesn't seem to know what to do with "data:text/calendar" ... or perhaps I'm completely off base and just don't know what I'm doing.

I can supply all of the code if necessary.

Thanks!


回答1:


I found an answer that worked for me in the github issues for react-add-to-calendar:

var blob = new Blob([icsMSG], { type: 'text/calendar;charset=utf-8' });
window.navigator.msSaveOrOpenBlob(blob, 'download.ics');

Where icsMSG is a string containing the data that should be in your ICS file (everything starting from "BEGIN"). There is also an if statement you can use in another file from this project to test if the browser is an older version of IE:

if (typeof window !== "undefined" &&
    window.navigator.msSaveOrOpenBlob &&
    window.Blob) { }

This is working for me in Internet Explorer 11 without having to use a server to download the file.



来源:https://stackoverflow.com/questions/23286118/datatext-calendar-not-recognized-by-internet-explorer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!