Skip to content

Instantly share code, notes, and snippets.

@jeremysimmons
Forked from erkantaylan/multipart.cs
Last active June 20, 2024 17:44
Show Gist options
  • Save jeremysimmons/b88630ee9e75ea0dc4f851788d558a56 to your computer and use it in GitHub Desktop.
Save jeremysimmons/b88630ee9e75ea0dc4f851788d558a56 to your computer and use it in GitHub Desktop.

Revisions

  1. jeremysimmons revised this gist Jun 20, 2024. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions multipart.cs
    Original file line number Diff line number Diff line change
    @@ -48,7 +48,7 @@ public async Task Download(string url, string saveAs)
    }
    }

    private async void DownloadPart(string url, string saveAs, long start, long end)
    private async Task DownloadPartAsync(string url, string saveAs, long start, long end)
    {
    using (var httpClient = new HttpClient())
    using (var fileStream = new FileStream(saveAs, FileMode.Open, FileAccess.Write, FileShare.Write))
    @@ -57,7 +57,8 @@ private async void DownloadPart(string url, string saveAs, long start, long end)
    message.Headers.Add("Range", string.Format("bytes={0}-{1}", start, end));

    fileStream.Position = start;
    await httpClient.SendAsync(message).Result.Content.CopyToAsync(fileStream);
    var response = await httpClient.SendAsync(message);
    await response.Content.CopyToAsync(fileStream);
    }
    }
    }
  2. @subena22jf subena22jf revised this gist Mar 11, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions multipart.cs
    Original file line number Diff line number Diff line change
    @@ -11,8 +11,8 @@ internal class Program
    private static void Main(string[] args)
    {
    Task.Run(() => new Downloader().Download(
    "http://download.tuxfamily.org/notepadplus/6.6.9/npp.6.6.9.Installer.exe",
    "d:\\npp.6.6.9.Installer.exe"
    "url_file_download",
    "url_save_to"
    )).Wait();
    }
    }
  3. @subena22jf subena22jf created this gist Mar 11, 2015.
    64 changes: 64 additions & 0 deletions multipart.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Net.Http;
    using System.Threading.Tasks;

    namespace TestApp
    {
    internal class Program
    {
    private static void Main(string[] args)
    {
    Task.Run(() => new Downloader().Download(
    "http://download.tuxfamily.org/notepadplus/6.6.9/npp.6.6.9.Installer.exe",
    "d:\\npp.6.6.9.Installer.exe"
    )).Wait();
    }
    }

    public class Downloader
    {
    public async Task Download(string url, string saveAs)
    {
    var httpClient = new HttpClient();
    var response = await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, url));
    var parallelDownloadSuported = response.Headers.AcceptRanges.Contains("bytes");
    var contentLength = response.Content.Headers.ContentLength ?? 0;

    if (parallelDownloadSuported)
    {
    const double numberOfParts = 5.0;
    var tasks = new List<Task>();
    var partSize = (long)Math.Ceiling(contentLength / numberOfParts);

    File.Create(saveAs).Dispose();

    for (var i = 0; i < numberOfParts; i++)
    {
    var start = i*partSize + Math.Min(1, i);
    var end = Math.Min((i + 1)*partSize, contentLength);

    tasks.Add(
    Task.Run(() => DownloadPart(url, saveAs, start, end))
    );
    }

    await Task.WhenAll(tasks);
    }
    }

    private async void DownloadPart(string url, string saveAs, long start, long end)
    {
    using (var httpClient = new HttpClient())
    using (var fileStream = new FileStream(saveAs, FileMode.Open, FileAccess.Write, FileShare.Write))
    {
    var message = new HttpRequestMessage(HttpMethod.Get, url);
    message.Headers.Add("Range", string.Format("bytes={0}-{1}", start, end));

    fileStream.Position = start;
    await httpClient.SendAsync(message).Result.Content.CopyToAsync(fileStream);
    }
    }
    }
    }