WebClient.UploadProgressChanged 事件

定義

在非同步上傳作業成功傳輸部分或全部資料時發生。

public:
 event System::Net::UploadProgressChangedEventHandler ^ UploadProgressChanged;
public event System.Net.UploadProgressChangedEventHandler? UploadProgressChanged;
public event System.Net.UploadProgressChangedEventHandler UploadProgressChanged;
member this.UploadProgressChanged : System.Net.UploadProgressChangedEventHandler 
Public Custom Event UploadProgressChanged As UploadProgressChangedEventHandler 
Public Event UploadProgressChanged As UploadProgressChangedEventHandler 

事件類型

範例

下列程式碼範例示範如何為此事件設定事件處理常式。

// Sample call: UploadFileInBackground2("http://www.contoso.com/fileUpload.aspx", "data.txt")
void UploadFileInBackground2( String^ address, String^ fileName )
{
   WebClient^ client = gcnew WebClient;
   Uri ^uri = gcnew Uri(address);

   client->UploadFileCompleted +=
     gcnew UploadFileCompletedEventHandler (UploadFileCallback2);

   // Specify a progress notification handler.
   client->UploadProgressChanged +=
       gcnew UploadProgressChangedEventHandler( UploadProgressCallback );
   client->UploadFileAsync( uri, "POST", fileName );
   Console::WriteLine( "File upload started." );
}
// Sample call: UploadFileInBackground2("http://www.contoso.com/fileUpload.aspx", "data.txt")
public static void UploadFileInBackground2(string address, string fileName)
{
    WebClient client = new WebClient();
    Uri uri = new Uri(address);

    client.UploadFileCompleted += new UploadFileCompletedEventHandler(UploadFileCallback2);

    // Specify a progress notification handler.
    client.UploadProgressChanged += new UploadProgressChangedEventHandler(UploadProgressCallback);
    client.UploadFileAsync(uri, "POST", fileName);
    Console.WriteLine("File upload started.");
}
'  Sample call: UploadFileInBackground2("http:' www.contoso.com/fileUpload.aspx", "data.txt")
Public Shared Sub UploadFileInBackground2(ByVal address As String, ByVal fileName As String)

    Dim client As WebClient = New WebClient()
                Dim uri as Uri =  New Uri(address)
    AddHandler client.UploadFileCompleted, AddressOf UploadFileCallback2

    '  Specify a progress notification handler.
    AddHandler client.UploadProgressChanged, AddressOf UploadProgressCallback
    client.UploadFileAsync(uri, "POST", fileName)
    Console.WriteLine("File upload started.")
End Sub

下列程式碼範例示範這個事件的處理常式實作。

static void UploadProgressCallback(Object^ sender,
          UploadProgressChangedEventArgs^ e)
      {
          // Displays the operation identifier, and the transfer progress.
          Console::WriteLine("{0}    uploaded {1} of {2} bytes. {3} % complete...",
              (String ^)e->UserState,
              e->BytesSent,
              e->TotalBytesToSend,
              e->ProgressPercentage);
      }
static void DownloadProgressCallback(Object^ sender,
          DownloadProgressChangedEventArgs^ e)
      {
          // Displays the operation identifier, and the transfer progress.
          Console::WriteLine("{0}    downloaded {1} of {2} bytes. {3} % complete...",
              (String ^)e->UserState,
              e->BytesReceived,
              e->TotalBytesToReceive,
              e->ProgressPercentage);
      }
private static void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e)
{
    // Displays the operation identifier, and the transfer progress.
    Console.WriteLine("{0}    uploaded {1} of {2} bytes. {3} % complete...",
        (string)e.UserState,
        e.BytesSent,
        e.TotalBytesToSend,
        e.ProgressPercentage);
}
private static void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
{
    // Displays the operation identifier, and the transfer progress.
    Console.WriteLine("{0}    downloaded {1} of {2} bytes. {3} % complete...",
        (string)e.UserState,
        e.BytesReceived,
        e.TotalBytesToReceive,
        e.ProgressPercentage);
}
Private Shared Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs)

    '  Displays the operation identifier, and the transfer progress.
    Console.WriteLine("{0}    uploaded {1} of {2} bytes. {3} % complete...", _
     CStr(e.UserState), e.BytesSent, e.TotalBytesToSend, e.ProgressPercentage)
End Sub
Private Shared Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)

    '  Displays the operation identifier, and the transfer progress.
    Console.WriteLine("{0}    downloaded {1} of {2} bytes. {3} % complete...", _
     CStr(e.UserState), e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage)
End Sub

備註

每次非同步上傳進行時,都會引發此事件。 當上傳開始使用下列任一方法時,就會引發此事件。

方法 描述
UploadDataAsync Byte將陣列傳送至資源,而不封鎖呼叫執行緒。
UploadFileAsync 將本機檔案傳送至資源,而不封鎖呼叫執行緒。
UploadValuesAsync NameValueCollection將 傳送至資源,並傳回 Byte 包含任何回應的陣列,而不會封鎖呼叫執行緒。

UploadProgressChangedEventHandler是這個事件的委派。 類別 UploadProgressChangedEventArgs 會提供事件處理常式與事件資料。

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

適用於