在 Xamarin Forms 中下载带有进度条的文件

Download file with progress bar in Xamarin Forms

我正在尝试在 Xamarin Forms 中创建一个带有下载进度条的下载页面(PCL,因此 WebClient 不可用)。我使用了 Xamarin 的以下信息,但没有成功:

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

这是我当前的代码(带有工作进度条):

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

现在,我需要将文件保存到本地存储。但是,在此示例中,我没有获取文件内容,因此无法将其写入本地存储。我需要更改代码中的哪些内容才能实现此目的?

仅供参考:在这个例子中,我正在下载一张图片,但它的特征是 .pdf/.doc/.docx。

提前致谢。

BR, FG

您实际上是在 for 循环内将文件内容复制到您的 buffer。将该循环的每个 运行 的 buffer 内容连接到一个新的 byte[] fileContentBuffer 中,您可以访问可以保存在本地存储中的内容。

想到你的文章,我也解决了下载文件

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