改变:
wc.UploadFileAsync(myUri, "POST", filePath);
wc.UploadProgressChanged += new UploadProgressChangedEventHandler(wc_UploadProgressChanged);
wc.UploadFileCompleted += new UploadFileCompletedEventHandler(wc_UploadFileCompleted);
为了:
wc.UploadProgressChanged += new UploadProgressChangedEventHandler(wc_UploadProgressChanged);
wc.UploadFileCompleted += new UploadFileCompletedEventHandler(wc_UploadFileCompleted);
wc.UploadFileAsync(myUri, "POST", filePath);
在 UploadFileAsync 调用之后绑定事件,您必须首先绑定事件处理程序。
MSDN 示例:
// 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.");
}