今天在專案中要產生 *.ics 的日曆數據交換檔案提供下載
打鐵趁熱記錄一下,以免又要石沈大海
目前採用的是在 NuGet 的 Ical.Net 套件, 版本 v4.1.10
在 Windows 10 的行事曆開啟 *.ics 的呈現內容
範例程式 (採用 Web API)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CalController : ApiController | |
{ | |
public HttpResponseMessage Get() | |
{ | |
// 套件: https://www.nuget.org/packages/Ical.Net/ | |
CalendarEvent calEvent = new CalendarEvent | |
{ | |
DtStart = new CalDateTime(DateTime.Today), | |
DtEnd = new CalDateTime(DateTime.Today.AddHours(3)), | |
Summary = "會議標題", | |
Description = "會議簡述文字", | |
Location = "台中市" | |
}; | |
// 產生 iCalendar 格式 | |
var iCal = new Calendar(); | |
iCal.Events.Add(calEvent); | |
var serializer = new CalendarSerializer(); | |
string output = serializer.SerializeToString(iCal); | |
// 下載輸出 | |
var response = new HttpResponseMessage(HttpStatusCode.OK); | |
response.Content = new StringContent(output); | |
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/calendar"); | |
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); | |
response.Content.Headers.ContentDisposition.FileName = HttpUtility.UrlPathEncode($"ical_{DateTime.Now:yyyyMMdd}.ics"); | |
return response; | |
} | |
} |