Last active
January 27, 2024 06:48
-
-
Save subena22jf/3358b8609966203502a5 to your computer and use it in GitHub Desktop.
c# download multi part
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | |
"url_file_download", | |
"url_save_to" | |
)).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); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ramtinak 's code works, however, no matter the number of chunks, it always takes the same amount of time to download the whole file,
actually, i have another code who downloads in single thread, and takes exactly the same amount of time.
Therefore... it breaks the purpose of Parallel downloads: to save time.
2 Chunks should download in half the time than 1.
4 Chunks should download in half the time than 2.
...