Click here to Skip to main content
Click here to Skip to main content

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

, 9 Nov 2004
Rate this:
Please Sign up or sign in to vote.
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.

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

About the Author

Florent Boulanger
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 Pinmemberjustin.moses6-Feb-08 13:08 
GeneralMemorystream for email attachments PinmemberMinessence29-Apr-05 13:53 
GeneralCR's PinmemberSouthAfrican21-Jan-05 5:45 
GeneralRe: CR's PinmemberFlorent Boulanger2-Feb-05 11:50 
GeneralRe: CR's PinmemberJB12110-Oct-05 7:14 
GeneralRe: CR's PinmemberJB12110-Oct-05 7:34 
GeneralGood job and thanks Pinmembermcflee22-Nov-04 3:07 
QuestionWorks in Firefox and not in IE?? PinmemberCimedaca12-Nov-04 3:46 
AnswerRe: Works in Firefox and not in IE?? PinmemberFlorent Boulanger3-Dec-04 11:14 
General[Message Deleted] PinmemberDan Colasanti10-Nov-04 20:07 
GeneralRe: Source Code? Pinmembermcflee22-Nov-04 0:37 
GeneralWindow media host control in Visual Studio. Pinmemberkhoaduongminh21-Oct-04 16:32 
GeneralSimilar articles PinmemberChris Becker21-Oct-04 9:41 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 9 Nov 2004
Article Copyright 2004 by Florent Boulanger
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid