使用 javascript 或 jquery 创建 .ics 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19137089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create .ics file with javascript or jquery
提问by joshuahornby10
I am trying to create an .ics
file when a user clicks a button.
我正在尝试.ics
在用户单击按钮时创建一个文件。
So far the code I have is
到目前为止,我拥有的代码是
msgData1 = $('.start-time').text();
msgData2 = $('.end-time').text();
msgData3 = $('.Location').text();
var icsMSG = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//Our Company//NONSGML v1.0//EN\nBEGIN:VEVENT\nUID:[email protected]\nDTSTAMP:20120315T170000Z\nATTENDEE;CN=My Self ;RSVP=TRUE:MAILTO:[email protected]\nORGANIZER;CN=Me:MAILTO::[email protected]\nDTSTART:" + msgData1 +"\nDTEND:" + msgData2 +"\nLOCATION:" + msgData3 + "\nSUMMARY:Our Meeting Office\nEND:VEVENT\nEND:VCALENDAR";
$('.button').click(function(){
window.open( "data:text/calendar;charset=utf8," + escape(icsMSG));
});
This downloads a .ics
file but when I try to open this in iCal I am told it can not read the file.
这会下载一个.ics
文件,但是当我尝试在 iCal 中打开它时,我被告知它无法读取该文件。
回答by smartbart24
I used the ics.jssolution mentioned by InsanelyADHD.
我使用了InsanelyADHD提到的ics.js解决方案。
One problem with the solution was that Chrome did not detect the filetype correctly and tried to open the file as text with the editor.
该解决方案的一个问题是 Chrome 没有正确检测文件类型,并尝试使用编辑器将文件作为文本打开。
I changed the download function to open the text simply with:
我更改了下载功能以简单地打开文本:
window.open( "data:text/calendar;charset=utf8," + escape(calendar));
My fork on Github is icsFormatter.js
我在 Github 上的 fork 是icsFormatter.js
Regarding the license: I have contacted the original author to include a GPL - after that I will include one too.
关于许可:我已经联系了原作者以包含 GPL - 之后我也会包含一个。
回答by InsanelyADHD
There is an open source project for this:
为此有一个开源项目:
It's 100% JavaScript and worked on all modern browsers except for IE 9 and below.
它是 100% JavaScript,适用于除 IE 9 及更低版本之外的所有现代浏览器。
If you are interested in how they get the files working, you can check their source. They use the following libraries to perform the heavy lifting:
如果您对他们如何使文件工作感兴趣,可以查看他们的来源。他们使用以下库来执行繁重的工作:
回答by Arnaud Quillaud
You have two colon for the organizer address: "MAILTO::[email protected]"
组织者地址有两个冒号:“MAILTO::[email protected]”
If this does not solve the issue, you will have to show us the full ical stream, as it is received by iCal.
如果这不能解决问题,您必须向我们展示完整的 ical 流,因为它是由 iCal 接收的。
Finally, and assuming that start_time and end_time are using the correct format, for the location field, you may need to wrap lines (http://tools.ietf.org/html/rfc5545#section-3.1) and escape certain characters (http://tools.ietf.org/html/rfc5545#section-3.3.11). In other words, you may want to look at iCalendar libraries.
最后,假设 start_time 和 end_time 使用正确的格式,对于位置字段,您可能需要换行 ( http://tools.ietf.org/html/rfc5545#section-3.1) 并转义某些字符 ( http://tools.ietf.org/html/rfc5545#section-3.1 ://tools.ietf.org/html/rfc5545#section-3.3.11)。换句话说,您可能需要查看 iCalendar 库。
回答by RegisteredUser
I used icsFormatterwhich was mentioned and by smartbart24. But I was required to support IE down to version 7, so I had to tweak things a little.
我使用了smartbart24提到的icsFormatter。但是我被要求支持 IE 到版本 7,所以我不得不稍微调整一下。
To use icsGen, my fork of icsFormatter, you have to have php 4 or 5 installed on your webserver.
要使用icsGen,我的 icsFormatter 分支,您必须在您的网络服务器上安装 php 4 或 5。
回答by Andri
ics.js didn't work for me since i was loading the data asynchronously from my database. I then switched to ical.jscombined with the saveAs function from ics.js and it worked. If anyone here is having the same issues, feel free to text me then i will provide an example
ics.js 对我不起作用,因为我从我的数据库异步加载数据。然后我切换到ical.js并结合了 ics.js 的 saveAs 功能,并且它起作用了。如果这里有人遇到同样的问题,请随时给我发短信,然后我会提供一个例子