“The remote host closed the connection” in Response.OutputStream.Write

前端 未结 4 1525
清酒与你
清酒与你 2020-12-28 15:31

This code streams large files to our users:

                // Open the file.
            iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Ope         


        
相关标签:
4条回答
  • 2020-12-28 15:35

    That exception means that the client downloading the file broke the connection before the file had finished downloading. i.e. The client navigated to another page, or just closed the browser.

    I might try moving the if (Response.IsClientConnected) line after your iStream.Read. Even if you did that, I think there still might be a chance to receive this error if the connection is broken while the OutputStream.Write method is still working.

    0 讨论(0)
  • 2020-12-28 15:39

    There are a few different possible causes of this. I can think of three:

    One is filling the buffer with close to 2GB of data, but this shouldn't be the case here, since you are flushing regularly.

    Another is indeed that described in the answer you previously accepted. It's very hard to reproduce, so I wouldn't assume it was necessarily wrong.

    Another possible case, and the one I would bet on, is that the executionTimeout is exceeded, which would cause a ThreadAbortException at first, but this could in turn cause the failure of Flush() which would turn into the exception noted

    0 讨论(0)
  • 2020-12-28 15:43

    I am posting this answer because it might be help others and save some important time.

    In my case Response.Buffer = true in download method (on very first statement) solved the issue.

    Thanks

    0 讨论(0)
  • 2020-12-28 15:44

    Increase executionTimeout in httpRuntime element of web.config.

    If user is downloading large file on slow connection, request will eventually timeout.

    0 讨论(0)
提交回复
热议问题