In my Silverlight application I need to download large files. I am currently streaming this data from a byte array by calling an ASPX page on the same server hosting the Silverlight app. The ASPX Page_Load()
method looks like this:
protected void Page_Load(object sender, EventArgs e)
{
// we are sending binary data, not HTML/CSS, so clear the page headers
Response.Clear();
Response.ContentType = "Application/xod";
string filePath = Request["file"]; // passed in from Silverlight app
// ...
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
// send data 30 KB at a time
Byte[] t = new Byte[30 * 1024];
int bytesRead = 0;
bytesRead = fs.Read(t, 0, t.Length);
Response.BufferOutput = false;
int totalBytesSent = 0;
Debug.WriteLine("Commence streaming...");
while (bytesRead > 0)
{
// write bytes to the response stream
Response.BinaryWrite(t);
// write to output how many bytes have been sent
totalBytesSent += bytesRead;
Debug.WriteLine("Server sent total " + totalBytesSent + " bytes.");
// read next bytes
bytesRead = fs.Read(t, 0, t.Length);
}
}
Debug.WriteLine("Done.");
// ensure all bytes have been sent and stop execution
Response.End();
}
From the Silverlight app, I just hand off the uri to object that reads in the byte array:
Uri uri = new Uri("https://localhost:44300/TestDir/StreamDoc.aspx?file=" + path);
My question is... How do I stop this stream if the user cancels out? As it is now, if the user selects another file to download, the new stream will start and the previous one will continue to stream until it has completed.
I can't find a way to abort the stream once its been started.
Any help is greatly appriciated.
-Scott