我正在使用下面显示的UploadFilesAsync方法上传文件。
var wc = new WebClient();
Uri myUri = new Uri(uriString);
wc.UploadFileAsync(myUri, "POST", filePath);
wc.UploadProgressChanged += new UploadProgressChangedEventHandler(wc_UploadProgressChanged);
wc.UploadFileCompleted += new UploadFileCompletedEventHandler(wc_UploadFileCompleted);
我的UploadFileCompleted事件处理程序被调用,但由于某种原因,从不调用UploadProgressChanged事件处理程序。
任何想法为什么?
答案 0 :(得分:1)
变化:
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);
您正在绑定事件 AFTER 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.");
}
答案 1 :(得分:0)
您应首先添加eventHandlers,然后调用将触发它们的方法。
wc.UploadProgressChanged += new UploadProgressChangedEventHandler(wc_UploadProgressChanged);
wc.UploadFileCompleted += new UploadFileCompletedEventHandler(wc_UploadFileCompleted);
wc.UploadFileAsync(myUri, "POST", filePath);