【发布时间】:2020-05-01 19:55:24
【问题描述】:
您好,我在使用此代码下载文件时遇到问题。 当我下载像 jpg 或 png 这样的小文件时,效果很好, 但是当我尝试下载 tar、rar 或 zip 等大文件时 我的进度条有问题。
这是我的代码
WebClient client;
private void button1_Click(object sender, EventArgs e)
{
string url = textBox1.Text;
if (!string.IsNullOrEmpty(url))
{
Thread thread = new Thread(() =>
{
Uri uri = new Uri(url);
string filename = System.IO.Path.GetFileName(uri.AbsolutePath);
client.DownloadFileAsync(uri, Application.StartupPath + "/" + filename);
});
thread.Start();
}
}
and this in form load
client = new WebClient();
client.DownloadProgressChanged += Client_DownloadProgressChanged;
client.DownloadFileCompleted += Client_DownloadFileCompleted;
and this is the operator
private void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Invoke(new MethodInvoker(delegate ()
{
progressBar1.Minimum = 0;
double reciverd = double.Parse(e.BytesReceived.ToString());
double total = double.Parse(e.TotalBytesToReceive.ToString());
double precentage = e.ProgressPercentage;
label1.Text = $"Downloaded{string.Format("{0:0##}", precentage)}%";
//progressBar1.Value = int.Parse(Math.Truncate(precentage).ToString());
progressBar1.Value = (int)precentage;
}));
}
已编辑
现在进度条和下载正常,但是下载完成后它直接从 0 变为 100 并且看不到进度级别。
【问题讨论】:
-
评论不用于扩展讨论;这个对话是moved to chat。
标签: c# winforms file download progress-bar