I'm writing a helper method in C#/ASP.NET that streams a file to the browser, and I would like to be able to detect if any content has been written to the browser prior to clearing the response headers/content and sending file bytes. If a page that makes a call to my helper method is not set up correctly, it seems like it would be possible (likely even?) for "normal" page headers and content to get sent to the browser prior to my attempting to clear the whole response and start fresh with file data. So, is there any way to tell if data has already been sent?
Basically, I'm looking for something like the fake property BytesSent in this example:
if (response.BytesSent == 0)
{
response.ClearHeaders();
response.ClearContent();
response.Clear();
response.ContentType = "application/octet-stream";
response.AppendHeader("Content-Disposition", "attachment; filename=" + filename);
response.AppendHeader("Content-Length", new FileInfo(path).Length.ToString());
//
// etc. (stream the file)
//
}
else
{
// throw an exception (or handle the problem in some other way)
}