WebClient.UploadProgressChanged 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在异步上载操作成功转换部分或全部数据后发生。
public:
event System::Net::UploadProgressChangedEventHandler ^ UploadProgressChanged;
public event System.Net.UploadProgressChangedEventHandler? UploadProgressChanged;
public event System.Net.UploadProgressChangedEventHandler UploadProgressChanged;
member this.UploadProgressChanged : System.Net.UploadProgressChangedEventHandler
Public Custom Event UploadProgressChanged As UploadProgressChangedEventHandler
Public Event UploadProgressChanged As UploadProgressChangedEventHandler
事件类型
示例
下面的代码示例演示如何为此事件设置事件处理程序。
// Sample call: UploadFileInBackground2("http://www.contoso.com/fileUpload.aspx", "data.txt")
void UploadFileInBackground2( String^ address, String^ fileName )
{
WebClient^ client = gcnew WebClient;
Uri ^uri = gcnew Uri(address);
client->UploadFileCompleted +=
gcnew UploadFileCompletedEventHandler (UploadFileCallback2);
// Specify a progress notification handler.
client->UploadProgressChanged +=
gcnew UploadProgressChangedEventHandler( UploadProgressCallback );
client->UploadFileAsync( uri, "POST", fileName );
Console::WriteLine( "File upload started." );
}
// Sample call: UploadFileInBackground2("http://www.contoso.com/fileUpload.aspx", "data.txt")
public static void UploadFileInBackground2(string address, string fileName)
{
WebClient client = new WebClient();
Uri uri = new Uri(address);
client.UploadFileCompleted += new UploadFileCompletedEventHandler(UploadFileCallback2);
// Specify a progress notification handler.
client.UploadProgressChanged += new UploadProgressChangedEventHandler(UploadProgressCallback);
client.UploadFileAsync(uri, "POST", fileName);
Console.WriteLine("File upload started.");
}
' Sample call: UploadFileInBackground2("http:' www.contoso.com/fileUpload.aspx", "data.txt")
Public Shared Sub UploadFileInBackground2(ByVal address As String, ByVal fileName As String)
Dim client As WebClient = New WebClient()
Dim uri as Uri = New Uri(address)
AddHandler client.UploadFileCompleted, AddressOf UploadFileCallback2
' Specify a progress notification handler.
AddHandler client.UploadProgressChanged, AddressOf UploadProgressCallback
client.UploadFileAsync(uri, "POST", fileName)
Console.WriteLine("File upload started.")
End Sub
下面的代码示例演示此事件的处理程序的实现。
static void UploadProgressCallback(Object^ sender,
UploadProgressChangedEventArgs^ e)
{
// Displays the operation identifier, and the transfer progress.
Console::WriteLine("{0} uploaded {1} of {2} bytes. {3} % complete...",
(String ^)e->UserState,
e->BytesSent,
e->TotalBytesToSend,
e->ProgressPercentage);
}
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);
}
private static void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e)
{
// Displays the operation identifier, and the transfer progress.
Console.WriteLine("{0} uploaded {1} of {2} bytes. {3} % complete...",
(string)e.UserState,
e.BytesSent,
e.TotalBytesToSend,
e.ProgressPercentage);
}
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);
}
Private Shared Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs)
' Displays the operation identifier, and the transfer progress.
Console.WriteLine("{0} uploaded {1} of {2} bytes. {3} % complete...", _
CStr(e.UserState), e.BytesSent, e.TotalBytesToSend, e.ProgressPercentage)
End Sub
Private Shared Sub DownloadProgressCallback(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
注解
每次异步上传进度时,都会引发此事件。 使用以下任一方法启动上传时,将引发此事件。
方法 | 说明 |
---|---|
UploadDataAsync | 在不 Byte 阻止调用线程的情况下,将数组发送到资源。 |
UploadFileAsync | 在不阻止调用线程的情况下,将本地文件发送到资源。 |
UploadValuesAsync | 将 发送到 NameValueCollection 资源并返回包含 Byte 任何响应的数组,而不会阻止调用线程。 |
是 UploadProgressChangedEventHandler 此事件的委托。 类 UploadProgressChangedEventArgs 为事件处理程序提供事件数据。
有关如何处理事件的详细信息,请参阅 处理和引发事件。
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈