我正在 C#/ASP.NET 中编写一个将文件流式传输到浏览器的辅助方法,并且我希望能够在清除响应标头/内容和发送文件之前检测是否有任何内容已写入浏览器字节。如果调用我的辅助方法的页面设置不正确,那么在我尝试清除之前,“正常”页面标题和内容似乎有可能(甚至可能?)整个响应并从文件数据重新开始。那么,有什么方法可以判断数据是否已经发送?
基本上,我在这个例子中寻找类似假属性 BytesSent 的东西:
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)
}