Last active
October 27, 2021 15:32
-
-
Save conficient/3051a99d1b03705619393930928ab057 to your computer and use it in GitHub Desktop.
ICalendar link generator
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
@using Ical.Net | |
@using Ical.Net.Serialization | |
@* requires Ical.Net library from NUGET *@ | |
@if(Calendar != null) | |
{ | |
<a href=@dataUrl download=@Filename >@ChildContent</a> | |
} | |
@code | |
{ | |
[Parameter] public Calendar Calendar { get; set; } | |
[Parameter] public RenderFragment ChildContent {get;set;} | |
[Parameter] public string Filename { get; set; } = "Calendar.ics"; | |
string dataUrl => GetDataUrl(); | |
string GetDataUrl() | |
{ | |
var serializer = new CalendarSerializer(); | |
var text = serializer.SerializeToString(Calendar); | |
var data = System.Text.Encoding.UTF8.GetBytes(text); | |
var base64 = Convert.ToBase64String(data); | |
return $"data:text/calendar;base64,{base64}"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've updated the gist to use base64 encoding rather than using URL encoding, as it does not corrupt the description or summary, etc.