WebClient.DownloadProgressChanged 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当异步下载操作成功传输部分或所有数据时发生。
public:
event System::Net::DownloadProgressChangedEventHandler ^ DownloadProgressChanged;
public event System.Net.DownloadProgressChangedEventHandler? DownloadProgressChanged;
public event System.Net.DownloadProgressChangedEventHandler DownloadProgressChanged;
member this.DownloadProgressChanged : System.Net.DownloadProgressChangedEventHandler
Public Custom Event DownloadProgressChanged As DownloadProgressChangedEventHandler
Public Event DownloadProgressChanged As DownloadProgressChangedEventHandler
事件类型
示例
下面的代码示例演示如何为 DownloadProgressChanged
事件设置事件处理程序。
// Sample call : DownLoadFileInBackground4 ("http://www.contoso.com/logs/January.txt");
void DownLoadFileInBackground4( String^ address )
{
WebClient^ client = gcnew WebClient;
Uri ^uri = gcnew Uri(address);
// Specify a DownloadFileCompleted handler here...
// Specify a progress notification handler.
client->DownloadProgressChanged += gcnew DownloadProgressChangedEventHandler( DownloadProgressCallback4 );
client->DownloadFileAsync( uri, "serverdata.txt" );
}
static void DownloadProgressCallback4(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);
}
// Sample call : DownLoadFileInBackground4 ("http://www.contoso.com/logs/January.txt");
public static void DownLoadFileInBackground4(string address)
{
WebClient client = new WebClient();
Uri uri = new Uri(address);
// Specify a DownloadFileCompleted handler here...
// Specify a progress notification handler.
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback4);
client.DownloadFileAsync(uri, "serverdata.txt");
}
private static void DownloadProgressCallback4(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);
}
' Sample call : DownLoadFileInBackground4 ("http://www.contoso.com/logs/January.txt");
Public Shared Sub DownLoadFileInBackground4(ByVal address As String)
Dim client As WebClient = New WebClient()
' Specify a DownloadFileCompleted handler here...
' Specify a progress notification handler.
AddHandler client.DownloadProgressChanged, AddressOf DownloadProgressCallback4
Dim uri as Uri = New Uri(address)
client.DownloadFileAsync(uri, "serverdata.txt")
End Sub
Private Shared Sub DownloadProgressCallback4(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
' Displays the operation identifier, and the transfer progress.
Console.WriteLine("{0} downloaded {1} of {2} bytes. {3} % complete...", _
CStr(e.UserState), e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage)
End Sub
注解
谨慎
WebRequest
、HttpWebRequest
、ServicePoint
和 WebClient
已过时,不应将其用于新开发。 请改用 HttpClient。
每次异步下载进行时都会引发此事件。 当使用以下任一方法开始下载时,将引发此事件。
方法 | 描述 |
---|---|
DownloadDataAsync | 从资源下载数据并返回 Byte 数组,而不会阻止调用线程。 |
DownloadFileAsync | 将数据从资源下载到本地文件,而不会阻止调用线程。 |
OpenReadAsync | 从资源返回数据,而不阻止调用线程。 |
DownloadProgressChangedEventHandler 是此事件的委托。 DownloadProgressChangedEventArgs 类为事件处理程序提供事件数据。
有关如何处理事件的详细信息,请参阅 处理和引发事件。
注意
被动 FTP 文件传输将始终显示进度百分比为零,因为服务器未发送文件大小。 若要显示进度,可以通过重写 GetWebRequest(Uri) 虚拟方法将 FTP 连接更改为活动状态:
internal class MyWebClient : WebClientProtocol
{
protected override WebRequest GetWebRequest(Uri address)
{
FtpWebRequest req = (FtpWebRequest)base.GetWebRequest(address);
req.UsePassive = false;
return req;
}
}