Search code examples
c#asp.netdday

How to make .ics file as downloadable c#


For Exporting of .ics file i wrote the following code. Collapse | Copy Code

iCalendar iCal = new iCalendar();
            foreach (DataRow row in dt.Rows)
            {
                // Create the event, and add it to the iCalendar
                Event evt = iCal.Create<event>();

            // Set information about the event
            evt.Start = (DDay.iCal.iCalDateTime)Convert.ToDateTime(row["StartDate"]);
            evt.End = (DDay.iCal.iCalDateTime)Convert.ToDateTime(row["EndDate"]);// This also sets the duration
            evt.Description = (string)row["Description"];
        }


     // Serialize (save) the iCalendar
 iCalendarSerializer serializer = new iCalendarSerializer();
        serializer.Serialize(iCal, @"C:\iCalendar.ics");

It is working fine for me but it writes file into C drive or as per the path we have given. but i need it to be downloadable.

i tried some code but it is not the exact format i needed.

Thanks in advance.


Solution

  • The documentation supports using a stream. You could write the contents of the calendar to the response. Make sure you set the mime type as well.

    iCalendarSerializer serializer = new iCalendarSerializer(iCal);
    serializer.Serialize(Response.OutputStream, Encoding.ASCII);
    

    http://www.ddaysoftware.com/Pages/Projects/DDay.iCal/Documentation/