WebClient.UploadDataCompleted Kejadian
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Terjadi ketika operasi unggahan data asinkron selesai.
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
Jenis Acara
Contoh
Contoh kode berikut menunjukkan pengaturan penanganan aktivitas untuk kejadian ini.
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
Contoh kode berikut menunjukkan implementasi handler untuk kejadian ini.
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
Keterangan
Hati
WebRequest
, HttpWebRequest
, ServicePoint
, dan WebClient
usang, dan Anda tidak boleh menggunakannya untuk pengembangan baru. Gunakan HttpClient sebagai gantinya.
Kejadian ini dinaikkan setiap kali operasi pengunggah data asinkron selesai. Unggahan data asinkron dimulai dengan memanggil metode UploadDataAsync.
UploadDataCompletedEventHandler adalah delegasi untuk peristiwa ini. Kelas UploadDataCompletedEventArgs menyediakan penanganan aktivitas dengan data peristiwa.
Untuk informasi selengkapnya tentang cara menangani peristiwa, lihat Menangani dan Meningkatkan Peristiwa.