I have a requirement for an application that I'm supporting to be able to parse the following URI.
data:text/calendar;charset=utf8,BEGIN:VCALENDAR[calendarContent]END:VCALENDAR
The calendar content I've indicated here is a serialized representation of what an ICS (calendar file) would contain - I'm omitting it for brevity/confidentiality.
For this, I've created a custom protocol handler for that data: protocol. I've confirmed that my ProcessRequestAsync() is correctly intercepting the request and saving the necessary information as a stream.
However, I'm at a loss for how to correctly handle the downloading of the ICS stream to a downloads folder (similar to what Chrome would do in this instance). Here is what I have so far:
- Code: Select all
public override CefReturnValue ProcessRequestAsync(IRequest request, ICallback callback)
{
Task.Run(() =>
{
using (callback)
{
Stream stream = null;
string vcal =
request.Url.Substring(request.Url.IndexOf("BEGIN:VCALENDAR"),
request.Url.Length - request.Url.IndexOf("BEGIN:VCALENDAR"));
stream = GetMemoryStream(Uri.UnescapeDataString(vcal), Encoding.UTF8);
if (stream == null)
{
callback.Cancel();
}
else
{
stream.Position = 0;
ResponseLength = stream.Length;
MimeType = "text/calendar;charset=utf8";
StatusCode = (int)HttpStatusCode.OK;
Stream = stream;
callback.Continue();
}
}
});
return CefReturnValue.ContinueAsync;
}
When I use a FileStream from the System.IO namespace, I can save it to my hard disk at the specified location, but I wonder if there's a better way to do this? I'm aiming for the cleanest approach possible that would be behave as closely as possible to how Chrome would handle this.