[C#] 使用 iCal.NET 產生 .ICS 行事曆事件+附加檔案
2023-02-07
今天接續之前透過 iCal.NET 來建立 .ics 的行事曆預約檔案的延伸
我們天才的業主希望在事件裡面加入 COUPON 圖片,我原本以為不行的想說婉拒
但是我後來查了一下好像可以,測試一下竟然成功,那就筆記一下
如果不知道 iCal.NET 的可以參考我上篇文章 使用 iCal.NET 產生 .ICS 行事曆事件+鬧鐘 ,這一篇是延續
主要就是建立事件後加上一張圖片,其實說穿了就是轉 base64
但是必須要加入 X-FILENAME 這去宣告檔名
This file contains 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
var newEvent = DateTime.Now.AddDays(7); | |
var title = " XXX 工作室預約"; | |
var evt = new Ical.Net.CalendarComponents.CalendarEvent | |
{ | |
DtStart = new Ical.Net.DataTypes.CalDateTime(newEvent, "Asia/Taipei"), | |
Start = new Ical.Net.DataTypes.CalDateTime(newEvent, "Asia/Taipei"), | |
//事件在十分鐘後結束 | |
DtEnd = new Ical.Net.DataTypes.CalDateTime(newEvent.AddMinutes(10), "Asia/Taipei"), | |
End = new Ical.Net.DataTypes.CalDateTime(newEvent.AddMinutes(10), "Asia/Taipei"), | |
Location = "台北市中正區中山南路21號", | |
Description = "親,這是您的預約 ,別忘記了", | |
IsAllDay = false, | |
Summary = title | |
}; | |
//轉base64 | |
byte[] imageArray = System.IO.File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + "demo6.jpg"); | |
var binaryAttachment = new Ical.Net.DataTypes.Attachment(imageArray); | |
//加入事件 | |
binaryAttachment.Parameters.Add("X-FILENAME", "demo6.jpg"); | |
evt.Attachments.Add(binaryAttachment); | |
var calendar = new Ical.Net.Calendar(); | |
calendar.Properties.Add(new Ical.Net.CalendarProperty("X-WR-CALNAME", title)); | |
calendar.AddTimeZone("Asia/Taipei"); | |
calendar.Events.Add(evt); | |
var serializer = new Ical.Net.Serialization.CalendarSerializer(); | |
var serializedCalendar = serializer.SerializeToString(calendar); | |
Result:
refernece:
https://stackoverflow.com/questions/29104715/attaching-a-file-to-an-icalendar