Web浏览器无法下载ics文件

问题描述 投票:0回答:1

浏览器没有下载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
c# .net icalendar
1个回答
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); 
© www.soinside.com 2019 - 2024. All rights reserved.