title: C# Download big file from Server with less memory consumption
body: <p>I have a big file of memory size 42 mb. I want to download the file with less memory consumption.<br>
  Controller Code</p>
  
  <pre><code>public ActionResult Download()
  {
      var filePath = "file path in server";
      FileInfo file = new FileInfo(filePath);
      Response.ContentType = "application/zip";                        
      Response.AppendHeader("Content-Disposition", "attachment; filename=folder.zip");                   
      Response.TransmitFile(file.FullName);
      Response.End(); 
  }
  </code></pre>
  
  <p>alernative method tried with <strong>Stream</strong><br></p>
  
  <pre><code>public ActionResult Download()
  {           
      string failure = string.Empty;
      Stream stream = null;
      int bytesToRead = 10000;
  
  
      long LengthToRead;
      try
      {
          var path = "file path from server";
          FileWebRequest fileRequest = (FileWebRequest)FileWebRequest.Create(path);
          FileWebResponse fileResponse = (FileWebResponse)fileRequest.GetResponse();
  
          if (fileRequest.ContentLength &gt; 0)
              fileResponse.ContentLength = fileRequest.ContentLength;
  
          //Get the Stream returned from the response
          stream = fileResponse.GetResponseStream();
  
          LengthToRead = stream.Length;
  
          //Indicate the type of data being sent
          Response.ContentType = "application/octet-stream";
  
          //Name the file 
          Response.AddHeader("Content-Disposition", "attachment; filename=SolutionWizardDesktopClient.zip");
          Response.AddHeader("Content-Length", fileResponse.ContentLength.ToString());
  
          int length;
          do
          {
              // Verify that the client is connected.
              if (Response.IsClientConnected)
              {
                  byte[] buffer = new Byte[bytesToRead];
  
                  // Read data into the buffer.
                  length = stream.Read(buffer, 0, bytesToRead);
  
                  // and write it out to the response's output stream
                  Response.OutputStream.Write(buffer, 0, length);
  
                  // Flush the data
                  Response.Flush();
  
                  //Clear the buffer
                  LengthToRead = LengthToRead - length;
              }
              else
              {
                  // cancel the download if client has disconnected
                  LengthToRead = -1;
              }
          } while (LengthToRead &gt; 0); //Repeat until no data is read
  
      }
      finally
      {
          if (stream != null)
          {
              //Close the input stream                   
              stream.Close();
          }
          Response.End();
          Response.Close();
      }
      return View("Failed");
  }
  </code></pre>
  
  <p>due to size of the file, it is consumpting more memory which leads to performance issue.<br>
  After checking in iis log, the download process is taking 42 mb and 64 mb each respectively.<br>
  Thanks in advance</p>