views:

489

answers:

1

i am using Webclient to upload data using Async call to a server,

    WebClient webClient = new WebClient();
   webClient.UploadDataAsync(uri , "PUT", buffer, userToken);

i've attached DatauploadProgress and DatauploadCompleted Events to appropriate callback functions

        // Upload Date Progress
        webClient.UploadProgressChanged += new 
        UploadProgressChangedEventHandler(UploadProgressCallback);

      // Upload Date Progress
     void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e)
    {
        // Magic goes here 
     logger.writeToLog("Percentage =" + e.ProgressPercentage);
    }

the e.ProgressPercentage always returns 50 .. no matter what the size of the file uploaded was (tried different sizes between 10kb to 60mb ). the function itself gets called only twice (really fast too) and percentage shows 50! ..which is illogical specially with big files ...

e.BytesSent doesn't help either..it always shows the files size in bytes :S (ex: if the file size was 63,000 , i'd get e.BytesSent = 63,000and e.ProgressPercentage= 50

Can someone point the problem out to me ?

+2  A: 

If you want to monitor the progress of an upload, you'll need to use UploadFileAsync instead of UploadData.

With UploadDataAsync you are supposed to manually chunk the file and display the progress (at least, that's what I've determined from my own experience in the matter though I haven't seen it written as such on MSDN).

What you're looking for is to use UploadFileAsync instead, which will call the UploadProgressChanged event correctly. You can then view the event args properties BytesSent and TotalBytesToSend which should be reflected correctly.

I assume the rationale behind this is that when you're sending data, you can loop over chunks of your data stream and manually increment your progress tracker whereas with a file you cannot (.NET will manage the entire upload for you). Personally, I feel there is something fishy because there is no reason for the UploadProgressChanged event to be called with invalid information in case of UploadDataAsync - either it is called with valid, correct info or it's not called at all.

At any rate, give UploadFileAsync a shot and see how that goes.

Computer Guru
+1 for taking your time to provide this detailed answer, i will test it and come back :)
Madi D.
Thx , tested it , and it solved the problem :S but i find it kinda stupid and illogical..
Madi D.