【问题标题】:ics file is not downloadable by the web browserWeb 浏览器无法下载 ics 文件
【发布时间】:2019-08-24 20:16:09
【问题描述】:

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

【问题讨论】:

  • 请解释您所说的“未被浏览器下载”是什么意思。发生什么了?你有任何错误信息吗?由于尚不清楚实际出了什么问题,请花点时间用更多信息更新您的问题,然后我相信您会得到更有帮助的回复。
  • 我同意它看起来应该做某事;所以:实际发生了什么?例如,如果您启动“Fiddler” - 这里实际的 http 响应是什么?标题和内容看起来正确吗?即它是否具有您指定的内容类型和处置?响应正文是 ics 数据吗?
  • 请显示响应的 HTTP 标头。您可以在浏览器的开发工具中查看这些内容。
  • 这里是头响应:HTTP/1.1 200 OK Cache-Control: private Content-Length: 485 Content-Type: text/calendar; charset=utf-8 内容处置:附件;文件名=CalendarItem_6 Dight Avenue Balwyn North 3104.ics.ics X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcQXN1c1xEZXNrdG9wXEF1c3RyYWxpYV9Tb3VyY2VcQXVzdHJhbGlhXFBQUS5XZWJcU2VhcmNoXENyZWF0ZUlpanDUZ0?

标签: c# .net icalendar


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2016-12-22
    • 2012-08-26
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    相关资源
    最近更新 更多