下载非常大的文件会导致客户端每隔1:30-1:45分钟断开连接

本文关键字:30-1 45分钟 连接 断开 客户端 非常 文件 下载 | 更新日期: 2023-09-27 18:07:30

这是我下载文件的代码。

             Stream iStream = null;
            // Buffer to read 1K bytes in chunk:
            byte[] buffer = new Byte[ 4194304 ];
            // Length of the file:
            int length;
            // Total bytes to read:
            long dataToRead;
            // Identify the file to download including its path.
            string filepath = PathToFile;
            // Identify the file name.
            //string filename = Path.GetFileName( filepath );
                             Response.Clear();
            try
            {
                // Open the file.
                iStream = new FileStream( filepath, FileMode.Open,
                            FileAccess.Read, FileShare.Read );

                // Total bytes to read:
                dataToRead = iStream.Length;
                Response.ContentType = ContentType;
                Response.AddHeader("Content-Disposition", "attachment; filename=" + clientNameOfFile);
                Response.AddHeader("Content-Length", dataToRead.ToString());
                Response.BufferOutput = true;
                // 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, 100000 );
                        // 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[ 100000];
                        dataToRead = dataToRead - length;
                    }
                    else
                    {
                        //prevent infinite loop if user disconnects
                        dataToRead = -1;
                    }
                }
            }
            catch ( Exception ex )
            {
                // Trap the error, if any.
                Response.Write( "Error : " + ex.Message );
            }
            finally
            {
                if ( iStream != null )
                {
                    //Close the file.
                    iStream.Close();
                }
                Response.Close();
            }

当尝试下载一个50MB的文件时,客户端在每1:30- 1:45分钟后断开连接。我应该检查哪些东西会导致客户端断开连接?

I have the following web config settings
<httpRuntime executionTimeout="999999" maxRequestLength="51200" />
<sessionState mode="StateServer" stateConnectionString="xxx:42424"   useHostingIdentity="true" timeout="10000" />
<requestFiltering>
            <requestLimits maxAllowedContentLength="209715200" />
        </requestFiltering>

设置IIS应用程序池空闲超时时间为20。

下载非常大的文件会导致客户端每隔1:30-1:45分钟断开连接

您的代码不一致。你已经声明了byte[] buffer = new byte[4194304];并将其改为buffer = new Byte[100000];在while循环中。长度也不一致length = iStream。