Downloading large files using WebRequest in C#

Downloading large files using WebRequest in C#

Fetch large files by consuming api through WebRequest

·

1 min read

Downloading large files especially videos is easy when you do it in chunks. Here's how:

 //file download in chunks...

    DateTime startTime = DateTime.UtcNow;
    WebRequest request = WebRequest.Create(newUrl);
    request.Headers.Add("Authorization", token);
    WebResponse fileResponse = request.GetResponse();

    using (Stream responseStream = fileResponse.GetResponseStream())
    {
      using (Stream fileStream = System.IO.File.OpenWrite(filePath))
      {
            byte[] buffer = new byte[4096];
            int bufferSize = (int)Math.Min(512 * 512, fileResponse.ContentLength);
            buffer = new byte[bufferSize];
            int bytesRead = responseStream.Read(buffer, 0, bufferSize);
            while (bytesRead > 0)
            {
                fileStream.Write(buffer, 0, bytesRead);
                DateTime nowTime = DateTime.UtcNow;
                if ((nowTime - startTime).TotalMinutes > 5)
                {
                    log.Error("Video Download timed out - 5 minutes");
                    throw new ApplicationException("Download timed out");
                }
                bytesRead = responseStream.Read(buffer, 0, bufferSize);
            }
            log.Info("Video Download time -" + (DateTime.UtcNow - startTime).TotalSeconds + " filename:" + filePath);
        }
    }

You can adjust the chunk size as per your file size.