2

我正在 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)
}
4

1 回答 1

0

我不知道您寻求的财产是否存在,但一般来说,您不会这样做。基本上,这种调用不是您希望从“页面”进行的调用,如果您指的是System.Web.UI.Page(通常是 .aspx 文件)的实例。相反,请使用处理程序 (.ashx) 进行此类工作。处理程序是 的实现System.Web.IHttpHandler,您应该从方法中调用此ProcessRequest()方法(或者只是将您的逻辑放入 中ProcessRequest())。

于 2012-03-06T05:12:48.953 回答