0

这是我下载文件的代码。

             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。

4

1 回答 1

0

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

于 2013-12-13T10:16:18.167 回答