共用方式為


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 類別會提供事件處理程式與事件數據。

如需如何處理事件的詳細資訊,請參閱 處理和引發事件

適用於