[英]ics file is not downloadable by the web browser
瀏覽器沒有下載Ics文件,我使用iCal.NET生成ics文件,代碼沒問題但是ics文件無法下載。 如何將其作為附件下載? 我的意思是如何使.ics文件可下載,以便我可以將其下載到我的磁盤,然后通過Outlook將其打開以將其添加到日歷中。
我的代碼:
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");
在瀏覽器響應中,我得到以下內容是正確的:
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
這是我的解決方案對我有用:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.