Internet Explorer 6.0IEVisual Studio .NET 2002QAVisual Studio .NET 2003HTMLIntermediateDevVisual StudioWindows.NETVisual BasicASP.NET
Generating vCalendar files (.vcs) to download in ASP.NET






3.24/5 (13 votes)
Oct 20, 2004

116263
VB.NET code for webpage to ask to download calendar items. Can be used for any calendars that supports vCalendar such as Outlook.
Introduction
Very simple to create vCalendar files, it is basically like creating text files, the only difference is the format.
In this example, a physical file is not created (no need to clean up a temp directory afterwards), the file is kept in memory using memory stream. Once the file is created in memory, it is sent for download.
vCalendar is using UTC, so the function ToUniversalTime
is used to convert the local time to 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>