Ошибка загрузки файла HTTPHandler на HttpResponse.Flush - PullRequest
0 голосов
/ 12 апреля 2011

У меня есть HTTPHandler, который отправляет файл клиенту.Я вижу ошибки, которые регистрируются только иногда.Файл, загружаемый во всех случаях, довольно маленький, менее 1 МБ.Вот ошибка / трассировка стека:

Удаленный хост закрыл соединение.Код ошибки 0x800703E3.

в System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError (результат Int32, логический throwOnDisconnect)
в System.Web.Hosting.IIS7WorkerRequest.ExplicitF.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?

...