Contribution Events/Download.ashx

Topics: Developer Discussion
Dec 29, 2007 at 2:50 PM
Edited Dec 29, 2007 at 2:52 PM
Happy new year !
This is an event handler that works with Club Starter Kit, Have Fun !
The actual released one does not.

<%@ WebHandler Language="C#" Class="DownloadEvent" %>

using System;
using System.Text;
using System.IO;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using SubSonic;
using ClubStarterKit.Data;

public class DownloadEvent : IHttpHandler
{
const int BUFFERSIZE = 1024;

public bool IsReusable
{
get
{
return true;
}
}

public void ProcessRequest( HttpContext context )
{
HttpResponse response = context.Response;
HttpRequest request = context.Request;
response.BufferOutput = true;
response.ContentType = "text/calendar";
response.Cache.SetCacheability( HttpCacheability.NoCache );
int eventID = Convert.ToInt32( request.QueryString "EventID" );

Uri viewurl = new Uri( context.Request.Url, "Events_view.aspx?Eventid=" + eventID );
writeCalEntry( eventID, response.Output, viewurl.ToString() );

response.AppendHeader( "Content-Disposition", "attachment; filename=EventCalendar" + eventID.ToString() + ".vcs" );
response.ContentType = "application/download";
response.End();
}

public void writeCalEntry( int EventID, TextWriter output, string url )
{
Query qry = new Query( Tables.ClubEvent );
qry.AddWhere( ClubEvent.Columns.Id, EventID );
qry.QueryType = QueryType.Select;
qry.Top = "1";

DataTable dt = new DataTable();
dt.Load( qry.ExecuteReader() );

if ( dt.Rows.Count > 0 )
{
DataRow dr = dt.Rows 0 ;

System.DateTime starttime;
System.DateTime endtime;
string title;
string description;
string location;
StringBuilder sb = new StringBuilder();
object o;

starttime = ( DateTime )dr "starttime" ;
if ( dr "endtime" != DBNull.Value )
{
endtime = ( DateTime )dr "endtime" ;
}
else
{
endtime = starttime;
}

if ( dr "title" != DBNull.Value )
{
title = ( string )( dr "title" );
}
else
{
title = "An untitled clubsite event";
}

o = dr "description" ;
if ( o != DBNull.Value && ( string )o != "" )
{
sb.AppendLine( ( string )o );
}
sb.AppendLine();
sb.Append( "Event URL: " );
o = dr "staticURL" ;
if ( o != DBNull.Value && ( string )o != "" )
{
sb.AppendLine( ( string )o );
}
else
{
sb.AppendLine( url );
}

o = dr "LocationName" ;
if ( o != DBNull.Value && ( string )o != "" )
{
location = ( string )o;
sb.AppendLine();
sb.Append( "Event location: " );
sb.AppendLine( location );
}
else
{
location = null;
}

o = dr "LocationDirections" ;
if ( o != DBNull.Value && ( string )o != "" )
{
sb.AppendLine();
sb.AppendLine( "Location description:" );
sb.AppendLine( ( string )o );
}

UnicodeEncoding uc = new UnicodeEncoding();
description = sb.ToString();

output.WriteLine( "BEGIN:VCALENDAR" );
output.WriteLine( "VERSION:2.0" );
output.WriteLine( "PRODID:" + url );
output.WriteLine( "METHOD:PUBLISH" );
output.WriteLine( "BEGIN:VEVENT" );
output.WriteLine( "UID:" + Guid.NewGuid().ToString() );
output.WriteLine( "DTSTAMP:" + DateTime.Now.ToUniversalTime().ToString( "yyyyMMddTHHmmss" ) + "Z" );
output.WriteLine( "CATEGORIES:APPOINTMENT;PERSONAL" );
output.WriteLine( EncodeProperty( "DESCRIPTION;ENCODING=QUOTED-PRINTABLE:", description ) );
output.WriteLine( "DTEND:" + endtime.ToUniversalTime().ToString( "yyyyMMddTHHmmss" ) + "Z" );
if ( !( location == null ) )
{
output.WriteLine( EncodeProperty( "LOCATION;ENCODING=QUOTED-PRINTABLE:", location ) );
}
output.WriteLine( "PRIORITY:0" );
output.WriteLine( "DTSTART:" + starttime.ToUniversalTime().ToString( "yyyyMMddTHHmmss" ) + "Z" );
output.WriteLine( "STATUS:NEEDS ACTION" );
output.WriteLine( EncodeProperty( "SUMMARY;ENCODING=QUOTED-PRINTABLE:", title ) );
output.WriteLine( "URL:" + url );
output.WriteLine( "END:VEVENT" );
output.WriteLine( "END:VCALENDAR" );
}
}

string EncodeProperty( string key, string value )
{
StringBuilder sb = new StringBuilder();
sb.Append( key );
sb.Append( value );
sb.Replace( ";", "\\;", key.Length, sb.Length - key.Length );
sb.Replace( "\r\n", "=0D=0A" );
int offset = 76;
while ( offset < sb.Length )
{
int pos = sb.ToString( offset - 5, 10 ).IndexOf( "=0D=0A" );
if ( pos == -1 )
{
sb.Insert( offset - 5 + pos, "=" + "\r\n" );
offset += 79 - 5 + pos;
}
else
{
sb.Insert( offset, "=" + "\r\n" );
offset += 79;
}
}
return sb.ToString();
}

string EncodeProperty64( string key, string value )
{
StringBuilder sb = new StringBuilder();
sb.Append( key );
UnicodeEncoding uc = new UnicodeEncoding();
sb.Append( System.Convert.ToBase64String( uc.GetBytes( value ), Base64FormattingOptions.None ) );

int offset = 76;

while ( offset < sb.Length )
{
sb.Insert( offset, "\r\n" );
offset += 78;
}
return sb.ToString();
}
}
Jan 5, 2008 at 12:05 AM
Anyone got a VB version?
Jan 5, 2008 at 8:02 PM
Hi halls,

It is an event handler .ashx so it works as well in VB or in C# you don't need anything else. Remember the CLR Common Language Rutime makes all languages compatible :
so Download.ashx
But with this one when you click on the button 'Import to my personal calendar it works making a file .cvs

Have fun
http://www.sodevlog.fr/asp.net.blog