WebClient.UploadDataCompleted 事件

定义

异步数据上传操作完成时发生。

public:
 event System::Net::UploadDataCompletedEventHandler ^ UploadDataCompleted;
public event System.Net.UploadDataCompletedEventHandler? UploadDataCompleted;
public event System.Net.UploadDataCompletedEventHandler UploadDataCompleted;
member this.UploadDataCompleted : System.Net.UploadDataCompletedEventHandler 
Public Custom Event UploadDataCompleted As UploadDataCompletedEventHandler 
Public Event UploadDataCompleted As UploadDataCompletedEventHandler 

事件类型

示例

下面的代码示例演示如何为此事件设置事件处理程序。

void UploadDataInBackground3( String^ address )
{
   WebClient^ client = gcnew WebClient;
   Uri ^uri = gcnew Uri(address);
   String^ text = "Time = 12:00am temperature = 50";
   array<Byte>^data = System::Text::Encoding::UTF8->GetBytes( text );

   client->UploadDataCompleted += gcnew UploadDataCompletedEventHandler( UploadDataCallback3 );
   client->UploadDataAsync( uri, data );
}
public static void UploadDataInBackground3(string address)
{
    WebClient client = new WebClient();
    Uri uri = new Uri(address);
    string text = "Time = 12:00am temperature = 50";
    byte[] data = System.Text.Encoding.UTF8.GetBytes(text);

    client.UploadDataCompleted += new UploadDataCompletedEventHandler(UploadDataCallback3);
    client.UploadDataAsync(uri, data);
}
Public Shared Sub UploadDataInBackground3(ByVal address As String)

    Dim client As WebClient = New WebClient()
    Dim text As String = "Time = 12:00am temperature = 50"
    Dim data() As Byte = System.Text.Encoding.UTF8.GetBytes(text)
    AddHandler client.UploadDataCompleted, AddressOf UploadDataCallback3
                Dim uri as Uri = New Uri(address)
    client.UploadDataAsync(uri, data)
End Sub

下面的代码示例演示此事件的处理程序的实现。

void UploadDataCallback3( Object^ /*sender*/, UploadDataCompletedEventArgs^ e )
{
   array<Byte>^data = dynamic_cast<array<Byte>^>(e->Result);
   String^ reply = System::Text::Encoding::UTF8->GetString( data );
   Console::WriteLine( reply );
}
private static void UploadDataCallback3(Object sender, UploadDataCompletedEventArgs e)
{
    byte[] data = (byte[])e.Result;
    string reply = System.Text.Encoding.UTF8.GetString(data);

    Console.WriteLine(reply);
}
Private Shared Sub UploadDataCallback3(ByVal sender As Object, ByVal e As UploadDataCompletedEventArgs)
    Dim data() As Byte = CType(e.Result, Byte())
    Dim reply As String = System.Text.Encoding.UTF8.GetString(data)
    Console.WriteLine(reply)
End Sub

注解

谨慎

WebRequestHttpWebRequestServicePointWebClient 已过时,不应将其用于新开发。 请改用 HttpClient

每次异步数据上传操作完成时都会引发此事件。 异步数据上传是通过调用 UploadDataAsync 方法启动的。

UploadDataCompletedEventHandler 是此事件的委托。 UploadDataCompletedEventArgs 类为事件处理程序提供事件数据。

有关如何处理事件的详细信息,请参阅 处理和引发事件

适用于