title: Response.WriteFile function fails and gives 504 gateway time-out
body: <p><strong>Environment:</strong><br>
  -SharePoint 2010 foundation<br>
  -Claim based authentication<br>
  -Execution time out in web.config is set to 3600<br></p>
  
  <p><strong>Overview:</strong><br>
  We have an excel export functionality where we connect to AD and SQL databases to fetch Users and their related data for a perticular Organization Unit (OU)in Active Directory.
  We have on OU in  AD which has got around 1400 users in it. We are using Open and Closed xml to generate excel file which works fine and takes about 11-14 minutes to generate a file on the server on following path
  C:\inetpub\wwwroot\wss\VirtualDirectories\VirtualDirectyrName\Excel\FileName.xlsx</p>
  
  <p>Immediately after generating a file we have following piece of code which would read file from server and dump it on output steam and presents a file ope-save as dialog box in browser to end user.</p>
  
  <p><strong>Problem Description:</strong><br>
  When an Organization has less number of users and it does not take more than 5-6 minteus to generate the file on server, following piece of code successfully downloads the file on browser. But when for above mentioned OU where we have 1400 users the reponse.writefile function fails and in browse we get to see 'Browse can not display this web page' (when fiddler was on we found it gives - http 504 error). Surpricingly if we perform this export from the server itself (i.e browse the web site on server) it downloads without issue. </p>
  
  <pre><code>protected void lnkbtnDownloadFile_Click(object sender, EventArgs e)
  {
  
      String fileName = @"C:\inetpub\wwwroot\wss\VirtualDirectories\VirtualDirectyrName\Excel\540KBFileWhichFails.xlsx";
          //File size is hardly ~500 KB
          //Wait for around 12 minutes, to mimic a scenario of file generation which takes time on staging and prod. environment.
          System.Threading.Thread.Sleep(720000);
          try
          {
          if (fileName != "")
                  {
                      var file = new FileInfo(fileName);
  
                      if (file.Exists)
                      {
                          Response.Clear();
                          Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                          Response.AddHeader("Content-Length", file.Length.ToString());
                          Response.ContentType = "application/octet-stream";
                          Response.WriteFile(file.FullName);
                          Response.End();
                      }
                      else
                          Response.Write("This file does not exist.");
                  }
              }
              catch (Exception ex)
              {
                  //This would usually give thread aboart exception but thats expected.
              }
  }
  </code></pre>
  
  <p>we dont see any error in ULS logs, event logs specific to this behavior.
  Please note , response.TransmitFile also gives same behaviour.</p>
  
  <p>any idea ?</p>