How to create a file in memory then send the file to the user through asp.net mvc + jquery?
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 n开发者_Python百科ot 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?
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;
}
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:19970901T130000Z-123402@host.com
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 https://www.rfc-editor.org/rfc/rfc2445
If you do this you wouldn't need to create a file at any time.
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);
}
精彩评论