UploadFileCompletedEventHandler 代理人
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表將處理 UploadFileCompleted 之 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)
參數
- sender
- Object
事件的來源。
範例
下列程式代碼範例示範如何以異步方式上傳檔案。
// 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
上傳完成時會呼叫下列方法。
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
備註
建立 UploadFileCompletedEventHandler 委派時,必須識別處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。 如需事件處理程式委派的詳細資訊,請參閱 處理和引發事件。
擴充方法
GetMethodInfo(Delegate) |
取得表示特定委派所代表之方法的物件。 |