title: Download large file in small chunks in C#
body: <p>I need to download some file which is more than 25 MB large, but my network only allow to request a file of 25 MB only.</p>
  
  <p>I am using following code </p>
  
  <pre><code>  const long DefaultSize = 26214400;
      long Chunk = 26214400;
      long offset = 0;
      byte[] bytesInStream;
      public void Download(string url, string filename)
      {
          long size = Size(url);
          int blocksize = Convert.ToInt32(size / DefaultSize);
          int remainder = Convert.ToInt32(size % DefaultSize);
          if (remainder &gt; 0) { blocksize++; }
  
          FileStream fileStream = File.Create(@"D:\Download TEST\" + filename);
          for (int i = 0; i &lt; blocksize; i++)
          {
              if (i == blocksize - 1)
              {
                  Chunk = remainder;
  
              }
  
              HttpWebRequest req = (HttpWebRequest)System.Net.WebRequest.Create(url);
              req.Method = "GET";
              req.AddRange(Convert.ToInt32(offset), Convert.ToInt32(Chunk+offset));
              HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
              // StreamReader sr = new StreamReader(resp.GetResponseStream());
  
  
              using (Stream responseStream = resp.GetResponseStream())
              {
                  bytesInStream = new byte[Chunk];
                  responseStream.Read(bytesInStream, 0, (int)bytesInStream.Length);
                  // Use FileStream object to write to the specified file
                  fileStream.Seek((int)offset, SeekOrigin.Begin);
                  fileStream.Write(bytesInStream,0, bytesInStream.Length);
              }
              offset += Chunk;
  
          }
          fileStream.Close();
  
      }
      public long Size(string url)
      {
          System.Net.WebRequest req = System.Net.HttpWebRequest.Create(url);
          req.Method = "HEAD";
          System.Net.WebResponse resp = req.GetResponse();
          resp.Close();
          return resp.ContentLength;
  
      }
  </code></pre>
  
  <p>It is properly writing content on disk but content is not working</p>