WebClient DownloadFileAsync - How can I display download speed to the user?

馋奶兔 提交于 2019-12-30 05:06:06

问题


That's pretty much the whole question in the title. I have a WPF C# Windows application, I download files for the user and now want to display the speed.


回答1:


mWebClient.DownloadProgressChanged += (sender, e) => progressChanged(e.BytesReceived);
//...
DateTime lastUpdate;
long lastBytes = 0;

private void progressChanged(long bytes)
{
    if (lastBytes == 0)
    {
        lastUpdate = DateTime.Now;
        lastBytes = bytes;
        return;
    }

    var now = DateTime.Now;
    var timeSpan = now - lastUpdate;
    var bytesChange = bytes - lastBytes;
    var bytesPerSecond = bytesChange / timeSpan.Seconds;

    lastBytes = bytes;
    lastUpdate = now;
}

And do whatever you need with the bytesPerSecond variable.




回答2:


We can do this easily by determining how many seconds have passed since the download is started. We can divide BytesReceived value from total seconds to get the speed. Take a look at the following code.

DateTime _startedAt;

WebClient webClient = new WebClient();

webClient.DownloadProgressChanged += OnDownloadProgressChanged;

webClient.DownloadFileAsync(new Uri("Download URL"), "Download Path")

private void OnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    if (_startedAt == default(DateTime))
    {
        _startedAt = DateTime.Now;
    }
    else
    {
        var timeSpan = DateTime.Now - _startedAt;
        if (timeSpan.TotalSeconds > 0)
        {
            var bytesPerSecond = e.BytesReceived / (long) timeSpan.TotalSeconds;
        }
    }
}



回答3:


Use the DownloadProgressChanged event

WebClient client = new WebClient ();
Uri uri = new Uri(address);

// Specify that the DownloadFileCallback method gets called
// when the download completes.
client.DownloadFileCompleted += new AsyncCompletedEventHandler (DownloadFileCallback2);
// Specify a progress notification handler.
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
client.DownloadFileAsync (uri, "serverdata.txt");

private static void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
{
    // Displays the operation identifier, and the transfer progress.
    Console.WriteLine("{0}    downloaded {1} of {2} bytes. {3} % complete...", 
        (string)e.UserState, 
        e.BytesReceived, 
        e.TotalBytesToReceive,
        e.ProgressPercentage);
}



回答4:


When you hook up the WebClient, you can subsribe to the ProgressChanged event, e.g.

_httpClient = new WebClient();
_httpClient.DownloadProgressChanged += DownloadProgressChanged;
_httpClient.DownloadFileCompleted += DownloadFileCompleted;
_httpClient.DownloadFileAsync(new Uri(_internalState.Uri), _downloadFile.FullName);

The EventArgs for this handler give you the BytesReceieved and TotalBytesToReceive. Using this information, you should be able to determine the download speed and shot a progress bar accordingly.



来源:https://stackoverflow.com/questions/11522577/webclient-downloadfileasync-how-can-i-display-download-speed-to-the-user

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!