问题
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