1

I'm uploading files using the UploadFilesAsync method shown below.

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);

My UploadFileCompleted event handler is called, but for some reason the UploadProgressChanged event handler is never called.

Any ideas why?

NMunro
  • 1,282
  • 4
  • 18
  • 33

2 Answers2

1

Change:

wc.UploadFileAsync(myUri, "POST", filePath);
wc.UploadProgressChanged += new UploadProgressChangedEventHandler(wc_UploadProgressChanged);
wc.UploadFileCompleted += new UploadFileCompletedEventHandler(wc_UploadFileCompleted);

For:

wc.UploadProgressChanged += new UploadProgressChangedEventHandler(wc_UploadProgressChanged);
wc.UploadFileCompleted += new UploadFileCompletedEventHandler(wc_UploadFileCompleted);
wc.UploadFileAsync(myUri, "POST", filePath);

You are binding the event AFTER the UploadFileAsync call, you must bind event handlers first.

MSDN EXAMPLE:

// 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.");
}
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82
0

You should first add eventHandlers and then call the method which will fire them.

wc.UploadProgressChanged += new UploadProgressChangedEventHandler(wc_UploadProgressChanged);
wc.UploadFileCompleted += new UploadFileCompletedEventHandler(wc_UploadFileCompleted);
wc.UploadFileAsync(myUri, "POST", filePath);
Nikola Davidovic
  • 8,556
  • 1
  • 27
  • 33