gpt4 book ai didi

C# WebClient DownloadProgressChanged 无法正常工作

转载 作者:太空狗 更新时间:2023-10-29 21:36:28 26 4
gpt4 key购买 nike

我正在尝试使用 C# 从 Internet 下载文件通过使用 WebClient 对象的 DownloadDataAsync 方法。

我还想通过使用 webclient 对象的 DownloadProgressChanged 事件来获取下载进度。

问题是,BytesReceived 和 TotalBytesToReceive 属性都没有显示正确的值。当我在调试时尝试检查它们时,它们都在以不可复制的方式发生变化。

我的代码:

WebClient client = new WebClient();
client.BaseAddress = this.DownloadUrl;
client.DownloadProgressChanged += downloadProgressDelegate;
client.DownloadDataAsync(new System.Uri(this.DownloadUrl));

最佳答案

我刚刚在 LINQPad 中尝试了以下代码:

var uri = @"http://download.services.openoffice.org/files/stable/3.3.0/OOo_3.3.0_Win_x86_install-wJRE_en-US.exe";

WebClient client = new WebClient();
client.DownloadProgressChanged += (sender, e) =>
{
Console.WriteLine("Bytes: " + e.BytesReceived + " of " + e.TotalBytesToReceive);
};

client.DownloadDataAsync(new System.Uri(uri));

Thread.Sleep(4000);
client.CancelAsync();

...并得到以下结果:

Bytes: 4138 of 158067944Bytes: 50858 of 158067944Bytes: 68378 of 158067944Bytes: 134078 of 158067944Bytes: 133914 of 158067944.....

似乎有效。

编辑:也许您的 HTTP 服务器没有 return the size正确,但我没有 URI 来针对 WebClient 实现测试此服务器行为。

关于C# WebClient DownloadProgressChanged 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4978023/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com