Dojnaz

Download w/ progbar

Feb 27th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net.Http;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace SmartHem
  10. {
  11.     public class Downloader : IDisposable
  12.     {
  13.         private readonly string _downloadUrl;
  14.         private readonly string _destinationFilePath;
  15.  
  16.         private HttpClient _httpClient;
  17.  
  18.         public delegate void ProgressChangedHandler(long? totalFileSize, long totalBytesDownloaded, double? progressPercentage);
  19.  
  20.         public event ProgressChangedHandler ProgressChanged;
  21.  
  22.         public Downloader(string downloadUrl, string destinationFilePath)
  23.         {
  24.             _downloadUrl = downloadUrl;
  25.             _destinationFilePath = destinationFilePath;
  26.         }
  27.  
  28.         public async Task StartDownload()
  29.         {
  30.             _httpClient = new HttpClient { Timeout = TimeSpan.FromDays(1) };
  31.  
  32.             using (var response = await _httpClient.GetAsync(_downloadUrl, HttpCompletionOption.ResponseHeadersRead))
  33.                 await DownloadFileFromHttpResponseMessage(response);
  34.         }
  35.  
  36.         private async Task DownloadFileFromHttpResponseMessage(HttpResponseMessage response)
  37.         {
  38.             response.EnsureSuccessStatusCode();
  39.  
  40.             var totalBytes = response.Content.Headers.ContentLength;
  41.  
  42.             using (var contentStream = await response.Content.ReadAsStreamAsync())
  43.                 await ProcessContentStream(totalBytes, contentStream);
  44.         }
  45.  
  46.         private async Task ProcessContentStream(long? totalDownloadSize, Stream contentStream)
  47.         {
  48.             var totalBytesRead = 0L;
  49.             var readCount = 0L;
  50.             var buffer = new byte[8192];
  51.             var isMoreToRead = true;
  52.  
  53.             using (var fileStream = new FileStream(_destinationFilePath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true))
  54.             {
  55.                 do
  56.                 {
  57.                     var bytesRead = await contentStream.ReadAsync(buffer, 0, buffer.Length);
  58.                     if (bytesRead == 0)
  59.                     {
  60.                         isMoreToRead = false;
  61.                         TriggerProgressChanged(totalDownloadSize, totalBytesRead);
  62.                         continue;
  63.                     }
  64.  
  65.                     await fileStream.WriteAsync(buffer, 0, bytesRead);
  66.  
  67.                     totalBytesRead += bytesRead;
  68.                     readCount += 1;
  69.  
  70.                     if (readCount % 100 == 0)
  71.                         TriggerProgressChanged(totalDownloadSize, totalBytesRead);
  72.                 }
  73.                 while (isMoreToRead);
  74.             }
  75.         }
  76.  
  77.         private void TriggerProgressChanged(long? totalDownloadSize, long totalBytesRead)
  78.         {
  79.             if (ProgressChanged == null)
  80.                 return;
  81.  
  82.             double? progressPercentage = null;
  83.             if (totalDownloadSize.HasValue)
  84.                 progressPercentage = Math.Round((double)totalBytesRead / totalDownloadSize.Value * 100, 2);
  85.  
  86.             ProgressChanged(totalDownloadSize, totalBytesRead, progressPercentage);
  87.         }
  88.  
  89.         public void Dispose()
  90.         {
  91.             _httpClient?.Dispose();
  92.         }
  93.     }
  94. }
Add Comment
Please, Sign In to add comment