UploadFileCompletedEventHandler UploadFileCompletedEventHandler UploadFileCompletedEventHandler UploadFileCompletedEventHandler Delegate

Definition

Stellt die Methode zur Behandlung des UploadFileCompleted-Ereignisses eines WebClient-Objekts dar.Represents the method that will handle the UploadFileCompleted event of a WebClient.

public delegate void UploadFileCompletedEventHandler(System::Object ^ sender, UploadFileCompletedEventArgs ^ e);
public delegate void UploadFileCompletedEventHandler(object sender, UploadFileCompletedEventArgs e);
type UploadFileCompletedEventHandler = delegate of obj * UploadFileCompletedEventArgs -> unit
Public Delegate Sub UploadFileCompletedEventHandler(sender As Object, e As UploadFileCompletedEventArgs)

Parameter

sender
Object Object Object Object

Die Quelle des Ereignisses.The source of the event.

Vererbung
UploadFileCompletedEventHandlerUploadFileCompletedEventHandlerUploadFileCompletedEventHandlerUploadFileCompletedEventHandler

Beispiele

Im folgenden Codebeispiel wird veranschaulicht, asynchron eine Datei hochzuladen.The following code example demonstrates asynchronously uploading a file.

// Sample call: UploadFileInBackground("http://www.contoso.com/fileUpload.aspx", "data.txt")
void UploadFileInBackground( String^ address, String^ fileName )
{
   System::Threading::AutoResetEvent^ waiter = gcnew System::Threading::AutoResetEvent( false );
   WebClient^ client = gcnew WebClient;
   Uri ^uri = gcnew Uri(address);
   String^ method = "POST";
   
   // Specify that that UploadFileCallback method gets called
   // when the file upload completes.
   client->UploadFileCompleted += gcnew UploadFileCompletedEventHandler( UploadFileCallback );
   client->UploadFileAsync( uri, method, fileName, waiter );
   
   // Block the main application thread. Real applications
   // can perform other tasks while waiting for the upload to complete.
   waiter->WaitOne();
   Console::WriteLine( "File upload is complete." );
}


// Sample call: UploadFileInBackground("http://www.contoso.com/fileUpload.aspx", "data.txt")
public static void UploadFileInBackground (string address, string fileName)
{
    System.Threading.AutoResetEvent waiter = new System.Threading.AutoResetEvent (false);
    WebClient client = new WebClient ();
    Uri uri = new Uri(address);
    string method = "POST";

    // Specify that that UploadFileCallback method gets called
    // when the file upload completes.
    client.UploadFileCompleted += new UploadFileCompletedEventHandler (UploadFileCallback);
    client.UploadFileAsync (uri, method, fileName, waiter);

    // Block the main application thread. Real applications
    // can perform other tasks while waiting for the upload to complete.
    waiter.WaitOne ();
    Console.WriteLine ("File upload is complete.");
}

'  Sample call: UploadFileInBackground("http:' www.contoso.com/fileUpload.aspx", "data.txt")
Public Shared Sub UploadFileInBackground(ByVal address As String, ByVal fileName As String)

	Dim waiter As System.Threading.AutoResetEvent = New System.Threading.AutoResetEvent(False)
	Dim client As WebClient = New WebClient()
	Dim method As String = "POST"
                       Dim uri as Uri =  New Uri(address)

	'  Specify that that UploadFileCallback method gets called
	'  when the file upload completes.
	AddHandler client.UploadFileCompleted, AddressOf UploadFileCallback
	client.UploadFileAsync(uri, method, fileName, waiter)

	'  Block the main application thread. Real applications
	'  can perform other tasks while waiting for the upload to complete.
	waiter.WaitOne()
	Console.WriteLine("File upload is complete.")
End Sub

Die folgende Methode wird aufgerufen, wenn der Upload abgeschlossen ist.The following method is called when the upload completes.

void UploadFileCallback( Object^ /*sender*/, UploadFileCompletedEventArgs^ e )
{
   System::Threading::AutoResetEvent^ waiter = dynamic_cast<System::Threading::AutoResetEvent^>(e->UserState);
   ;
   try
   {
      String^ reply = System::Text::Encoding::UTF8->GetString( e->Result );
      Console::WriteLine( reply );
   }
   finally
   {
      
      // If this thread throws an exception, make sure that
      // you let the main application thread resume.
      waiter->Set();
   }

}


private static void UploadFileCallback (Object sender, UploadFileCompletedEventArgs e)
{
    System.Threading.AutoResetEvent waiter = (System.Threading.AutoResetEvent)e.UserState;;
    try
    {
        string reply = System.Text.Encoding.UTF8.GetString (e.Result);

        Console.WriteLine (reply);
    }
    finally
    {
        // If this thread throws an exception, make sure that
        // you let the main application thread resume.
        waiter.Set ();
    }
}

Private Shared Sub UploadFileCallback(ByVal sender As Object, ByVal e As System.Net.UploadFileCompletedEventArgs)

    Dim waiter As System.Threading.AutoResetEvent = CType(e.UserState, System.Threading.AutoResetEvent)
    Try

        Dim reply As String = System.Text.Encoding.UTF8.GetString(e.Result)
        Console.WriteLine(reply)
    Finally
        '  If this thread throws an exception, make sure that
        '  you let the main application thread resume.
        waiter.Set()
    End Try
End Sub

Hinweise

Beim Erstellen eines UploadFileCompletedEventHandler-Delegaten bestimmen Sie die Methode für die Ereignisbehandlung.When you create a UploadFileCompletedEventHandler delegate, you identify the method that will handle the event. Um dem Ereignishandler das Ereignis zuzuordnen, fügen Sie dem Ereignis eine Instanz des Delegaten hinzu.To associate the event with your event handler, add an instance of the delegate to the event. Der Ereignishandler wird bei jedem Eintreten des Ereignisses aufgerufen, sofern der Delegat nicht entfernt wird.The event handler is called whenever the event occurs, unless you remove the delegate. Weitere Informationen zu den Ereignishandlerdelegaten, finden Sie unter behandeln und Auslösen von Ereignissen.For more information about event handler delegates, see Handling and Raising Events.

Extension Methods

GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate)

Ruft ein Objekt, das die durch den angegebenen Delegaten dargestellte Methode darstellt.Gets an object that represents the method represented by the specified delegate.

Gilt für: