Asp.Net - Downloading 50MB file from SQL server stops in middle for a while
When I'm trying to download a 50MB file from database (it is not an issue with small file size), it stops in the middle sometimes and resumes again after a long time. Am I mssing something? Thanks in advanced!

   

     SqlConnection con = new SqlConnection(ConnectionString);
        SqlCommand cmd = new SqlCommand("DownloadFile", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandTimeout = 60;
        
        cmd.Parameters.AddWithValue("@Id", Id);
        
        try
        {
            conPortal.Open();
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        
        
            // File name
            string fileName = reader.GetString(0);
        
            // Total bytes to read
            int dataToRead = reader.GetInt32(1);
        
            Context.Server.ScriptTimeout = 600;
            Context.Response.Buffer = true;
            Context.Response.Clear();
        
        
            Context.Response.ContentType = "application/octet-stream";
            Context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" 
    + fileName + "\";");
        
        
            Context.Response.AddHeader("Content-Length", dataToRead.ToString());
        
            int ChunkSize = 262144;
        
            // Buffer to read 10K bytes in chunk
            byte[] buffer = new Byte[ChunkSize];
            long offset = 0;
            long length;
        
            // Read the bytes.
            while (dataToRead > 0)
            {
                // Verify that the client is connected.
                if (Context.Response.IsClientConnected)
                {
                    // Read the data in buffer
                    length = reader.GetBytes(2, offset, buffer, 0, ChunkSize);
                    offset += ChunkSize;
        
                    // Write the data to the current output stream.
                    Context.Response.OutputStream.Write(buffer, 0, (int) length);
        
                    // Flush the data to the HTML output.
                    Context.Response.Flush();
        
                    buffer = new Byte[ChunkSize];
                    dataToRead = dataToRead - (int) length;
                }
                else
                {
                    //prevent infinite loop if user disconnects
                    dataToRead = -1;
                }
            }
        }
        finally
        {
            cmd.Dispose();
        }
Asp.Net - Downloading 50MB file from SQL server stops in middle for a while
===

When I'm trying to download a 50MB file from database (it is not an issue with small file size), it stops in the middle sometimes and resumes again after a long time. Am I mssing something? Thanks in advanced!

   

     SqlConnection con = new SqlConnection(ConnectionString);
        SqlCommand cmd = new SqlCommand("DownloadFile", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandTimeout = 60;
        
        cmd.Parameters.AddWithValue("@Id", Id);
        
        try
        {
            conPortal.Open();
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        
        
            // File name
            string fileName = reader.GetString(0);
        
            // Total bytes to read
            int dataToRead = reader.GetInt32(1);
        
            Context.Server.ScriptTimeout = 600;
            Context.Response.Buffer = true;
            Context.Response.Clear();
        
        
            Context.Response.ContentType = "application/octet-stream";
            Context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" 
    + fileName + "\";");
        
        
            Context.Response.AddHeader("Content-Length", dataToRead.ToString());
        
            int ChunkSize = 262144;
        
            // Buffer to read 10K bytes in chunk
            byte[] buffer = new Byte[ChunkSize];
            long offset = 0;
            long length;
        
            // Read the bytes.
            while (dataToRead > 0)
            {
                // Verify that the client is connected.
                if (Context.Response.IsClientConnected)
                {
                    // Read the data in buffer
                    length = reader.GetBytes(2, offset, buffer, 0, ChunkSize);
                    offset += ChunkSize;
        
                    // Write the data to the current output stream.
                    Context.Response.OutputStream.Write(buffer, 0, (int) length);
        
                    // Flush the data to the HTML output.
                    Context.Response.Flush();
        
                    buffer = new Byte[ChunkSize];
                    dataToRead = dataToRead - (int) length;
                }
                else
                {
                    //prevent infinite loop if user disconnects
                    dataToRead = -1;
                }
            }
        }
        finally
        {
            cmd.Dispose();
        }