Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using Xamarin.Forms;
- using System.Net.Http;
- using System.IO;
- using System.Threading.Tasks;
- namespace DownloadExample
- {
- public partial class DownloadPage : ContentPage
- {
- public DownloadPage ()
- {
- InitializeComponent ();
- DownloadFile("https://upload.wikimedia.org/wikipedia/commons/3/3d/LARGE_elevation.jpg");
- }
- private async Task<long> DownloadFile(string url)
- {
- long receivedBytes = 0;
- long totalBytes = 0;
- HttpClient client = new HttpClient ();
- using (var stream = await client.GetStreamAsync(url)) {
- byte[] buffer = new byte[4096];
- totalBytes = stream.Length;
- for (;;) {
- int bytesRead = await stream.ReadAsync (buffer, 0, buffer.Length);
- if (bytesRead == 0) {
- await Task.Yield ();
- break;
- }
- receivedBytes += bytesRead;
- int received = unchecked((int)receivedBytes);
- int total = unchecked((int)totalBytes);
- double percentage = ((float) received) / total;
- progressBar1.Progress = percentage;
- }
- }
- return receivedBytes;
- }
- }
- }
Add Comment
Please, Sign In to add comment