Guest User

Untitled

a guest
Jul 19th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. using Xamarin.Forms;
  5. using System.Net.Http;
  6. using System.IO;
  7. using System.Threading.Tasks;
  8.  
  9. namespace DownloadExample
  10. {
  11. public partial class DownloadPage : ContentPage
  12. {
  13. public DownloadPage ()
  14. {
  15. InitializeComponent ();
  16.  
  17. DownloadFile("https://upload.wikimedia.org/wikipedia/commons/3/3d/LARGE_elevation.jpg");
  18. }
  19.  
  20. private async Task<long> DownloadFile(string url)
  21. {
  22. long receivedBytes = 0;
  23. long totalBytes = 0;
  24. HttpClient client = new HttpClient ();
  25.  
  26. using (var stream = await client.GetStreamAsync(url)) {
  27. byte[] buffer = new byte[4096];
  28. totalBytes = stream.Length;
  29.  
  30. for (;;) {
  31. int bytesRead = await stream.ReadAsync (buffer, 0, buffer.Length);
  32. if (bytesRead == 0) {
  33. await Task.Yield ();
  34. break;
  35. }
  36.  
  37. receivedBytes += bytesRead;
  38.  
  39. int received = unchecked((int)receivedBytes);
  40. int total = unchecked((int)totalBytes);
  41.  
  42. double percentage = ((float) received) / total;
  43.  
  44. progressBar1.Progress = percentage;
  45. }
  46. }
  47.  
  48. return receivedBytes;
  49. }
  50. }
  51. }
Add Comment
Please, Sign In to add comment