C# - WebClient.TotalBytesToReceive返回-1始终为

时间:2016-11-23 11:29:56

标签: c# wpf asynchronous async-await

我在C#开发方面相对较新。

我正在尝试使用WebClient实现下载程序。

我所做的就是这个 -

private void startDownloadFile(string url)
{
    try
    {
        downloadProgress.Value = 0;
        downloadProgress.Visibility = Visibility.Visible;

        WebClient webClient = new WebClient();

        //Update UI with download progress
        webClient.DownloadProgressChanged += wcDownloadingString;

        webClient.DownloadStringAsync(new Uri(url), downloadFileLocation);
    }
    catch (Exception e)
    {
        // Stop the timer.
        timer.Enabled = false;
        MessageBox.Show(e.ToString());
        bDownload.IsEnabled = true;
        downloadProgress.Visibility = Visibility.Hidden;
    }
}

private void wcDownloadingString(object sender, DownloadProgressChangedEventArgs e)
{
    downloadProgress.Value = e.ProgressPercentage;
}

所以,我应该在进度条中获取用户界面更新,但我不会这样做,因为e.ProgressPercentage总是返回0

它返回0因为,WebClient.TotalBytesToReceive始终返回-1

enter image description here

我的完整代码可以在这里找到 -

https://github.com/abrarjahin/wpf-Download-Manager-With-Async-Task

有人可以帮忙吗?

重新 -

我去了这个地方 -

  1. https://social.msdn.microsoft.com/Forums/en-us/f5fe49aa-65ba-4ef9-a3b9-5ee6a2242829/why-totalbytestoreceive-is-always-1?forum=netfxnetcom
  2. c# WebClient DownloadProgresschanged TotalBytesToReceive = -1
  3. Download file from FTP with Progress - TotalBytesToReceive is always -1?
  4. 但是从那里找不到任何解决方案。

    Re Re -

    而且,服务器总是返回文件大小,因为我正在实现一个Web源下载器,并且它无法在任何地方工作,所以服务器没有问题。

0 个答案:

没有答案