Click here to Skip to main content
15,919,434 members
Articles / Web Development / HTML
Article

Generating vCalendar files (.vcs) to download in ASP.NET

Rate me:
Please Sign up or sign in to vote.
3.24/5 (13 votes)
9 Nov 2004 115.6K   53   13
VB.NET code for webpage to ask to download calendar items. Can be used for any calendars that supports vCalendar such as Outlook.

Sample Image - vcalendarfiletodownload.jpg

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.

VB
<%@ 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>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Broad engineering background with business knowledge (graduated from a top French engineering school training entrepreneurs), and excellent relationship skills that Flo utilizes in the I.T. field to streamline/improve company processes.

From web to embedded programming, from a big-5 consulting company to a local hospital, Flo shares his time between development and implementation lead.
Work with ASP.NET, VB.NET, LDAP, SQL Server, Oracle, MySQL...

Comments and Discussions

 
GeneralAuto Updates Pin
Justincc6-Feb-08 13:08
professionalJustincc6-Feb-08 13:08 
GeneralMemorystream for email attachments Pin
Minessence29-Apr-05 13:53
Minessence29-Apr-05 13:53 
GeneralCR's Pin
BitMite21-Jan-05 5:45
BitMite21-Jan-05 5:45 
GeneralRe: CR's Pin
Florent Boulanger2-Feb-05 11:50
Florent Boulanger2-Feb-05 11:50 
GeneralRe: CR's Pin
JB12110-Oct-05 7:14
JB12110-Oct-05 7:14 
GeneralRe: CR's Pin
JB12110-Oct-05 7:34
JB12110-Oct-05 7:34 
GeneralGood job and thanks Pin
mcflee22-Nov-04 3:07
mcflee22-Nov-04 3:07 
QuestionWorks in Firefox and not in IE?? Pin
Cimedaca12-Nov-04 3:46
Cimedaca12-Nov-04 3:46 
AnswerRe: Works in Firefox and not in IE?? Pin
Florent Boulanger3-Dec-04 11:14
Florent Boulanger3-Dec-04 11:14 
General[Message Deleted] Pin
Dan Colasanti10-Nov-04 20:07
professionalDan Colasanti10-Nov-04 20:07 
GeneralRe: Source Code? Pin
mcflee22-Nov-04 0:37
mcflee22-Nov-04 0:37 
GeneralWindow media host control in Visual Studio. Pin
Member 120566921-Oct-04 16:32
Member 120566921-Oct-04 16:32 
GeneralSimilar articles Pin
slolife21-Oct-04 9:41
slolife21-Oct-04 9:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.