views:

96

answers:

3

Hi

I am using Dday calendar library and I want to export the users calendars. So I use the Dday to take all the users records and make it into ical format(.ics).

Now I want to take this and send it back to the user. However I really don't want to first generate a file on the server then send it to them. If I can I rather do it in in memory and send it to them.

If it is however alot of work I will settle for a way that saves it on the server allows the user to download it and then deletes it after they finished downloading it(I don't want the file to be on my server for more then a few mins).

I also am not sure how to send it back the users. I always prefer to do an ajax post to the server with jquery then somehow have the file come back and popup for the user.

Again if this is alot of work I will settle for something server side way that asp.net mvc handles it all.

But I don't know how to do either way.

So how can I do this?

A: 

You need a server side handler, which will generate the data, set the HttpResponse.ContentType to the corresponding MIME type and then you'll need to write the data to the HttpResponse.ResponseStream or use HttpResponse.Write method.

Example (CalendarHandler.ashx):

public class CalendarHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/calendar";
        var calendarData = @"
 BEGIN:VEVENT
 UID:[email protected]
 DTSTAMP:19970901T1300Z
 DTSTART:19970401T163000Z
 DTEND:19970402T010000Z
 SUMMARY:Laurel is in sensitivity awareness class.
 CLASS:PUBLIC
 CATEGORIES:BUSINESS,HUMAN RESOURCES
 TRANSP:TRANSPARENT
 END:VEVENT";
        context.Response.Write(calendarData);
    }
}

Now you'll only need to make a request to CalcHandler.ashx using ajax or using conventional methods.

Note: i don't know if that's a valid candelar data, i just took a random example from the RFC http://tools.ietf.org/html/rfc2445

If you do this you wouldn't need to create a file at any time.

AlbertEin
Hi Dday returns a stream how do I write the stream. I don't see anything in context.Response.
chobo2
var cd = new ContentDisposition { Inline = false, FileName = "file.ics" }; Response.AppendHeader("Content-Disposition", cd.ToString());
Hurricanepkt
@chobo2 tell me which method do you use to get the calendar stream using the documentation: http://www.ddaysoftware.com/Pages/Projects/DDay.iCal/Documentation/ and i'll tell you exactly how to write the sream back to the client.
AlbertEin
+1  A: 

You need to return type FileStreamResult with MVC

public FileStreamResult MyFunction(/*your parms*/)
{
    System.IO.MemoryStream stream = FunctionThatCreatesMyCalendarStream();
    //you may need stream.Position = 0 here depending on what state above funciton leaves the stream in.
    //stream.Position = 0;
    FileStreamResult result = new FileStreamResult(stream, "text/calendar");
    result.FileDownloadName = "myfiledownloadname.ics";
    return result;
}
Russell Steen
I dont' think jquery though will know how render it as a file though
chobo2
pipe your call to a new browser window. jquery can do that, and that works. that's how i do it.
Russell Steen
<a href="fileiwanttodownload" target="_blank">File Download</a>
Russell Steen
A: 

You can use your own action result: (Got no idea what DCal does so making it up here)

public class DCalResult : ActionResult
{
    private readonly ISomeDCalInterface _dcalObject;

    public DCalResult(ISomeDCalInterface dcalObject)
    {
        _dcalObject = dcalObject;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        var response = context.HttpContext.Response;    
        response.ClearHeaders();
        response.ContentType = "text/calendar";
        response.Write(_dcalObject.ToString());
    }
}

Then in your controllers action return that:

public ActionResult DCal()
{
    return new DCalResult(usersDcalObject);
}
Maxwell Troy Milton King