The Ics file is not downloaded by the browser, I have used iCal.NET to generate ics file and the code is ok but the ics file is not downloadable. How can I download it as an attachment? I mean How to make .ics file as downloadable so I can download it to my disk and then open it by Outlook to add it to the calendar.
My code:
var calendar = new Ical.Net.Calendar();
calendar.Events.Add(new Event
{
Class = "PUBLIC",
Summary = Summary,
Created = new CalDateTime(DateTime.Now),
Description = title,
Start = new CalDateTime(Convert.ToDateTime(EventStart)),
End = new CalDateTime(Convert.ToDateTime(EventEnd)),
Sequence = 0,
Uid = Guid.NewGuid().ToString(),
Location = Location,
});
var serializer = new CalendarSerializer(new SerializationContext());
var serializedCalendar = serializer.SerializeToString(calendar);
var bytesCalendar = Encoding.UTF8.GetBytes(serializedCalendar);
return File(bytesCalendar, "text/calendar", "event.ics");
In the browser response I am getting the following which is correct:
BEGIN:VCALENDAR
PRODID:-//github.com/rianjs/ical.net//NONSGML ical.net 2.2//EN
VERSION:2.0
BEGIN:VEVENT
CLASS:PUBLIC
CREATED:20190403T191521
DESCRIPTION:Inspection of property: 6 Dight Avenue Balwyn North 3104
DTEND:20190413T110000
DTSTAMP:20190403T111521Z
DTSTART:20190413T103000
LOCATION:-37.797534\, 145.079921
SEQUENCE:0
SUMMARY:Inspection of property: 6 Dight Avenue Balwyn North 3104
UID:d977d4c7-2a0d-453e-940f-d7313e35b197
END:VEVENT
END:VCALENDAR
Here is my solution that worked for me:
StringBuilder sb = new StringBuilder();
//start the calendar item
sb.AppendLine("BEGIN:VCALENDAR");
sb.AppendLine("VERSION:2.0");
sb.AppendLine("PRODID:propertyqueen.com");
sb.AppendLine("CALSCALE:GREGORIAN");
sb.AppendLine("METHOD:PUBLISH");
//create a time zone if needed, TZID to be used in the event itself
sb.AppendLine("BEGIN:VTIMEZONE");
sb.AppendLine("BEGIN:STANDARD");
sb.AppendLine("TZID:Australia/Sydney");
sb.AppendLine("TZOFFSETTO:+1000");
sb.AppendLine("TZOFFSETFROM:+1000");
sb.AppendLine("END:STANDARD");
sb.AppendLine("END:VTIMEZONE");
//add the event
sb.AppendLine("BEGIN:VEVENT");
//without timezones
sb.AppendLine("DTSTART:" + EventStartDateTime.ToString("yyyyMMddTHHmm00"));
sb.AppendLine("DTEND:" + EventEndDateTime.ToString("yyyyMMddTHHmm00"));
sb.AppendLine("SUMMARY:" + Summary + "");
sb.AppendLine("LOCATION:" + Location + "");
sb.AppendLine("DESCRIPTION:" + Description + "");
sb.AppendLine("PRIORITY:3");
sb.AppendLine("END:VEVENT");
sb.AppendLine("END:VCALENDAR");
//send the calendar item to the browser
Response.ClearHeaders();
Response.Clear();
Response.Buffer = true;
Response.ContentType = "text/calendar";
Response.AddHeader("content-length", sb.Length.ToString());
Response.AddHeader("content-disposition", "attachment;filename=" + FileName);
Response.Write(sb.ToString());
Response.Flush();
Response.End();
var bytes = Encoding.UTF8.GetBytes(sb.ToString());
return File(bytes, "text/calendar", FileName);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.