我有一个HTTPHandler,它将文件发送到客户端。我看到有时只记录错误。在所有情况下下载的文件都很小,不到1MB。这是错误/堆栈跟踪:
远程主机关闭了连接。 错误代码是0x800703E3。
at System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result,Boolean throwOnDisconnect)
在System.Web.Hosting.IIS7WorkerRequest.ExplicitFlush() 在System.Web.HttpResponse.Flush(Boolean finalFlush)
以下是代码:
public class DownloadHttpHandler : IHttpHandler
{
public bool IsReusable { get { return true; } }
public void ProcessRequest(HttpContext context)
{
//Checking permission, getting the file path, etc...
ResponseUtil.SendDownloadFile(context.Response, fullPath);
}
}
public static class ResponseUtil
{
/// <summary>Sends the specified file.</summary>
public static void SendDownloadFile(HttpResponse response, string path, string contentType)
{
FileInfo fileInfo = new FileInfo(path);
BeginSendDownloadFile(response, fileInfo.Name, contentType, fileInfo.Length);
using (FileStream stream = File.OpenRead(path))
{
stream.CopyTo(response.OutputStream);
}
EndSendDownloadFile(response);
}
/// <summary>Prepares the output stream to send a downloadable file.</summary>
public static void BeginSendDownloadFile(HttpResponse response, string filename, string contentType, long contentLength)
{
if (response.IsClientConnected)
{
response.AddHeader("Content-Disposition", "attachment; filename={0}".FormatString(filename));
response.ContentType = contentType;
response.AddHeader("Content-Length", contentLength.ToString());
}
}
/// <summary>Flushes and closes the output stream.</summary>
public static void EndSendDownloadFile(HttpResponse response)
{
if (response.IsClientConnected)
{
response.Flush();
response.Close();
}
}
}
我想也许下载被取消所以我在几个地方添加了response.IsClientConnected
个支票。但我仍然看到错误。
我是否应该致电Flush
和Close
?