gpt4 book ai didi

c# - Response.OutputStream.Write 中的 "The remote host closed the connection"

转载 作者:IT王子 更新时间:2023-10-29 04:27:07 26 4
gpt4 key购买 nike

此代码将大文件流式传输给我们的用户:

                // Open the file.
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
System.IO.FileAccess.Read, System.IO.FileShare.Read);


// Total bytes to read:
dataToRead = iStream.Length;

// Read the bytes.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10000);

// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);

// Flush the data to the HTML output.
Response.Flush();

buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}

每隔一段时间我们就会收到这个异常:

The remote host closed the connection. The error code is 0x80072746

这是完整的堆栈跟踪:

Stack Trace:
at System.Web.Hosting.ISAPIWorkerRequestInProcForIIS6.FlushCore(Byte[] status, Byte[] header, Int32 keepConnected, Int32 totalBodySize, Int32 numBodyFragments, IntPtr[] bodyFragments, Int32[] bodyFragmentLengths, Int32 doneWithSession, Int32 finalStatus, Boolean& async)
at System.Web.Hosting.ISAPIWorkerRequest.FlushCachedResponse(Boolean isFinal)
at System.Web.Hosting.ISAPIWorkerRequest.FlushResponse(Boolean finalFlush)
at System.Web.HttpResponse.Flush(Boolean finalFlush)
at System.Web.HttpResponse.Flush()
at System.Web.HttpWriter.WriteFromStream(Byte[] data, Int32 offset, Int32 size)
at System.Web.HttpResponseStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at BIS.DocumentBus.Controls.DocumentViewer.StreamFile(String filepath)

我们从未有证据表明用户在下载我们的文件时遇到问题并打算忽略此异常。

知道这个问题的根源是什么吗?忽略它安全吗?

最佳答案

该异常意味着下载文件的客户端在文件下载完成之前断开了连接。即客户端导航到另一个页面,或者只是关闭浏览器。

我可能会尝试在 iStream.Read 之后移动 if (Response.IsClientConnected) 行。即使您这样做了,我认为如果在 OutputStream.Write 方法仍在工作时连接断开,我认为仍然有可能收到此错误。

关于c# - Response.OutputStream.Write 中的 "The remote host closed the connection",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/831447/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com