65.9K
CodeProject 正在变化。 阅读更多。
Home

在 ASP.NET 中生成 vCalendar 文件 (.vcs) 以供下载

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.24/5 (13投票s)

2004年10月20日

viewsIcon

116626

VB.NET 代码,用于网页请求下载日历项目。可用于支持 vCalendar 的任何日历,例如 Outlook。

Sample Image - vcalendarfiletodownload.jpg

引言

创建 vCalendar 文件非常简单,基本上就像创建文本文件一样,唯一的区别在于格式。

在此示例中,不会创建物理文件(之后无需清理临时目录),文件将使用内存流保存在内存中。文件在内存中创建后,将发送进行下载。

vCalendar 使用 UTC,因此使用函数 ToUniversalTime 将本地时间转换为 UTC。

<%@ Page Language="vb" ContentType="text/html" 
    ResponseEncoding="iso-8859-1" Debug="False" trace="False"%>
<%@ import Namespace="System.IO" %>

<script runat="server"> 
Sub Page_Load(Sender As Object, E As EventArgs) 

 'PARAMETERS

   Dim beginDate as Date = #01/07/2005 4:00 PM#

   Dim endDate as Date  = #01/07/2005 6:00 PM#
   Dim myLocation as String = "Computer Room"
   Dim mySubject as String = "Training"
   Dim myDescription as String = "Event details" 
 'INITIALIZATION

   Dim mStream As new MemoryStream()
   Dim writer As new StreamWriter(mStream)
   writer.AutoFlush = true 
 'HEADER

   writer.WriteLine("BEGIN:VCALENDAR")
   writer.WriteLine("PRODID:-//Flo Inc.//FloSoft//EN")
   writer.WriteLine("BEGIN:VEVENT") 
 'BODY

   writer.WriteLine("DTSTART:" & _
            beginDate.ToUniversalTime.ToString("yyyyMMdd\THHmmss\Z") )
   writer.WriteLine("DTEND:" & _
           endDate.ToUniversalTime.ToString("yyyyMMdd\THHmmss\Z") )
   writer.WriteLine("LOCATION:" & myLocation)
   writer.WriteLine("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" & myDescription)
   writer.WriteLine("SUMMARY:" & mySubject) 
 'FOOTER

   writer.WriteLine("PRIORITY:3")
   writer.WriteLine("END:VEVENT")
   writer.WriteLine("END:VCALENDAR") 
 'MAKE IT DOWNLOADABLE

   Response.Clear() 'clears the current output content from the buffer

   Response.AppendHeader("Content-Disposition", _
            "attachment; filename=Add2Calendar.vcs")
   Response.AppendHeader("Content-Length", mStream.Length.ToString())
   Response.ContentType = "application/download"
   Response.BinaryWrite(mStream.ToArray())
   Response.End() 
End Sub
</script>
© . All rights reserved.