2
我在C#/ ASP.NET中编写了一个助手方法,它将文件传输到浏览器,并且我希望能够检测在清除响应标头/内容之前是否有任何内容已经写入浏览器,以及发送文件字节。如果调用我的帮助器方法的页面设置不正确,那么在尝试清除“正常”页面标题和内容之前,似乎有可能(甚至可能?)发送到浏览器整个响应并从文件数据开始。那么,有什么办法可以判断数据是否已经发送?如何判断响应数据是否已发送给客户端,但在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)
}