ics文件无法通过网络浏览器下载

时间:2019-04-03 10:21:51

标签: c# .net icalendar

浏览器未下载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

1 个答案:

答案 0 :(得分:0)

这是对我有用的解决方案:

        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);