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)
}