4

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

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 working 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 to my local storage. But, in this example, I'm not getting the file content, so I can't write it to my local storage. What do I need to change in the code to make this possible?

FYI: In this example, I'm downloading an image, but it will be a .pdf / .doc / .docx in feature.

Thanks in advance.

BR, FG

F.G.
  • 75
  • 2
  • 9
  • possible duplicate of [How to implement progress reporting for Portable HttpClient](http://stackoverflow.com/questions/21169573/how-to-implement-progress-reporting-for-portable-httpclient) – Daniel Luberda Aug 18 '15 at 13:42
  • Can you share how you saved file and how did you concat buffer?... – Sanman Sep 13 '17 at 12:07

2 Answers2

1

Think your article,I also solved download file

WebClient client = new WebClient();
using (var stream = await client.OpenReadTaskAsync(Download point))
{
   using (MemoryStream ms = new MemoryStream())
   {
        var buffer = new byte[BufferSize];
        int read = 0;
        totalBytes = Int32.Parse(client.ResponseHeaders[HttpResponseHeader.ContentLength]);

       while ((read = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
        {
           ms.Write(buffer, 0, read);//important : receive every buffer
           receivedBytes += read;
           received = unchecked((int)receivedBytes);
           total = unchecked((int)totalBytes);

           percentage = ((float)received) / total;
           progressBar1.Progress = percentage;
           labProg.Text = AppResources.MsgDownloadprogress + Math.Truncate(percentage * 100).ToString() + "%";
        }//END while
        Stream ALLstream = new MemoryStream(ms.ToArray());//important change Stream
        App.APK.GenApkFile(ALLstream);//change APK file and save memory of phone
     }//END using (MemoryStream
  stream.Close();
}//END using (var stream
user10103506
  • 161
  • 1
  • 3
0

You actually copy the file content to your buffer inside the for loop. Concat the buffer content each run of that loop into a new byte[] fileContentBuffer and you have access to the contents which you can save in the local storage.

Wosi
  • 41,986
  • 17
  • 75
  • 82