title: Downloading a file raises exception
body: <p>I have a webform with a GridView which has filename as a column. If you click on filename, an Open/Save dialog box is presented to the user. Most of time, this raises an exception with the error message - <code>The remote host closed the connection. The error code is 0x80072746</code>.</p>
  
  <p>This error is not visible to the users in any other browser, except Firefox. In Firefox, the selected file gets rendered on the page itself, and after sometime, the page gives an error - The connection was reset.</p>
  
  <p>I am downloading the file by breaking it down to packets as I have to identify if file has been completely downloaded or not, and then write this entry to database table.</p>
  
  <p>I have put <code>Buffer="false"</code> in the page directive, but it does not work.
  I tried removing <code>Response.Flush()</code> from my code, but to no avail.</p>
  
  <p>My file download code is as follows:</p>
  
  <pre><code>LinkButton btnTemp = (LinkButton)sender;
  GridViewRow row = (GridViewRow)btnTemp.NamingContainer;
  HiddenFieldFullFileName.Value = row.Cells[1].Text;
  
  FileInfo file = new FileInfo(HiddenFieldFullFileName.Value);
  if (file.Exists)
  {
      string filePath="", fileName="";
  
      //store filepath and filename in separate variables
      string[] temp = row.Cells[1].Text.Split('\\');
      for (int j = 0; j &lt; temp.Length; j++)
      {
          if (j &lt; (temp.Length - 1))
              if(j==0)
                filePath = filePath + temp[j];
              else
                filePath = filePath + "\\" + temp[j];
          else
              fileName = temp[j];
  
      }
      FileStream myFile = new FileStream(row.Cells[1].Text, FileMode.Open,FileAccess.Read, FileShare.ReadWrite);
  
      //Reads file as binary values
      BinaryReader _BinaryReader = new BinaryReader(myFile);
  
      long startBytes = 0;
      string lastUpdateTimeStamp = File.GetLastWriteTimeUtc(filePath).ToString("r");
      string _EncodedData = HttpUtility.UrlEncode(fileName, Encoding.UTF8) + lastUpdateTimeStamp;
  
      //Clear the content of the response
      Response.Clear();
      Response.Buffer = false;
      Response.AddHeader("Accept-Ranges", "bytes");
      Response.AppendHeader("ETag", "\"" + _EncodedData + "\"");
      Response.AppendHeader("Last-Modified", lastUpdateTimeStamp);
  
      //Set the ContentType
      Response.ContentType = "application/octet-stream";
  
      //Add the file name and attachment,
      //which will force the open/cancel/save dialog to show, to the header
      Response.AddHeader("Content-Disposition", "attachment;filename=" + file.Name);
  
      //Add the file size into the response header
      Response.AddHeader("Content-Length", (file.Length - startBytes).ToString());
      Response.AddHeader("Connection", "Keep-Alive");
  
      //Set the Content Encoding type
      Response.ContentEncoding = Encoding.UTF8;
  
      //Send data
      _BinaryReader.BaseStream.Seek(startBytes, SeekOrigin.Begin);
  
      //Dividing the data in 1024 bytes package
      int maxCount = (int)Math.Ceiling((file.Length - startBytes + 0.0) / 1024);
  
      //Download in block of 1024 bytes
      int i;
      for (i = 0; i &lt; maxCount &amp;&amp; Response.IsClientConnected; i++)
      {
          Response.BinaryWrite(_BinaryReader.ReadBytes(1024));
          Response.Flush();
      }
  
      //compare packets transferred with total number of packets
      if (i &gt;= maxCount)
      {
          //get the IP address of user
          string ipAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
          if (ipAddress == null)
          {
              ipAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
          }
  
          //write the download information to database table
  
      }
  
  
      //Close Binary reader and File stream
      _BinaryReader.Close();
      myFile.Close();
  }
  </code></pre>
  
  <p>What is the source of the problem?</p>