Upload progress bar file to Xamarin Forms

I am trying to make a download page in Xamarin Forms (PCL, so WebClient is not used) with a progress bar. I used the following information from Xamarin, but to no avail:

http://developer.xamarin.com/recipes/ios/network/web_requests/download_a_file/ http://developer.xamarin.com/recipes/cross-platform/networking/download_progress/

This is my current code (with a progress bar):

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;
        }
    }
}

Now I need to save the file in local storage. But in this example, I do not get the contents of the file, so I can not write it to local storage. What do I need to change in the code to make this possible?

FYI: In this example, I upload an image, but it will be a .pdf / .doc / .docx function.

Thanks in advance.

BR, FG

+4
1

buffer for. buffer byte[] fileContentBuffer, , .

0

All Articles