Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Tried this with sendDataAsync, sendDataTaskAsync, and UploadFileTaskAsync, with the same result. When the code comes to `client.UploadFileTaskAsync` will be called and I can see by the `Console.Writeline` that it counts up to the size of the file I'm trying to send very quickly then the program will stand still for a minute and end. I can see that the value of `e.TotalBytesToSend` is always -1. I looked this up in MSDN but can't find anything about the meaning of -1. I tried doing this call with curl and using that I can see in fiddler that the file is being uploaded when curl is counting up the bytes sent so apparently it is possible to get it right, I just don't see how. 

This is probably related to the similar issue I posted about yesterday when I tried to get progress using `httpwebrequest` https://stackoverflow.com/questions/65391831/showing-file-upload-progress-with-httpwebrequest 

Here is a minimal program that I can reproduce the issue with.

What I have tried:

<pre lang="c#">class Program
{
   static async Task Main(string[] args)
   {
      using (var client = new WebClient())
      {
         client.UploadProgressChanged += UploadProgressChanged;
         var result = await client.UploadFileTaskAsync(
            "https://postman-echo.com/post", 
            "POST",
            "C:/Dev/Unity/AltspaceVr test word/template.zip");
      }
   }
   
   private static void UploadProgressChanged(
   object sender, 
   UploadProgressChangedEventArgs e)
   {
      Console.WriteLine("Sent: " + e.BytesSent + " Total: " + e.TotalBytesToSend);
   }
}
Posted
Updated 23-Dec-20 6:28am

1 solution

You're missing a few "status" fields; try the "official" example and see if it makes more sense.

WebClient.UploadProgressChanged Event (System.Net) | Microsoft Docs[^]
 
Share this answer
 
Comments
endasil 24-Dec-20 10:06am    
So this is what i output now:

Console.WriteLine("{0} uploaded {1} of {2} bytes. {3} % complete... {4:h:mm:ss}",
e.UserState,
e.BytesSent,
e.TotalBytesToSend,
e.ProgressPercentage,
DateTime.Now);

Using all the parameters from the example, e.UserState is always empty, e.BytesSent increases for each call, e.TotalBytesToSend is always -1 e.ProgressPercentage is 0 while it counts up to the
file size. Once it has counted up to the size of the file it will stand still for a while and then i can see in fiddler that it actually starts to upload the file. A few seconds later it will
show a bunch of calls where ProgressPercentage is 50% and e.BytesSent is the size of the file,
in fiddler, I can see that it is still doing uploads when doing this. So I can't see any way
of actually getting progress of how much it has uploaded, e.BytesSent is the same as the file size before it even starts transmitting the file.

"" uploaded 16535 of -1 bytes. 0 % complete... 1:44:28
"" uploaded 90263 of -1 bytes. 0 % complete... 1:44:28
"" uploaded 98455 of -1 bytes. 0 % complete... 1:44:28
"" uploaded 106647 of -1 bytes. 0 % complete... 1:44:28
"" uploaded 8343 of -1 bytes. 0 % complete... 1:44:28
"" uploaded 32919 of -1 bytes. 0 % complete... 1:44:28
"" uploaded 24727 of -1 bytes. 0 % complete... 1:44:28
"" uploaded 49303 of -1 bytes. 0 % complete... 1:44:28
"" uploaded 57495 of -1 bytes. 0 % complete... 1:44:28
"" uploaded 41111 of -1 bytes. 0 % complete... 1:44:28
"" uploaded 73879 of -1 bytes. 0 % complete... 1:44:28
"" uploaded 65687 of -1 bytes. 0 % complete... 1:44:28
"" uploaded 82071 of -1 bytes. 0 % complete... 1:44:28
"" uploaded 151 of -1 bytes. 0 % complete... 1:44:28
... CONTINUES LIKE THIS FOR ABOUT 1000 LINES...
"" uploaded 11460759 of -1 bytes. 0 % complete... 1:44:29
"" uploaded 11468951 of -1 bytes. 0 % complete... 1:44:29
"" uploaded 11472790 of -1 bytes. 0 % complete... 1:44:29
"" uploaded 11472834 of -1 bytes. 0 % complete... 1:44:29

Here it stops spitting out data at 1:44:29, then at 43 seconds the progress jumps up to 50% and it continues to show the same
uploaded byte number but with progress of 50% instead of 0. Googled this and it seems like it upload progress goes to max 50%
An upload or download tasks only goes to 50% per design according to https://social.msdn.microsoft.com/Forums/en-US/9ab5a1b4-c30d-4dbb-bebe-a00ef8e72029/problems-with-webclientuploadfileasync?forum=netfxnetcom

"The reason is that when you do an upload the server can also send data as a response to the upload. In general, this is true for any HTTP Request. Consider a form uploading a bunch of input and the server sending a whole bunch of data, perhaps an SQL server database report.
In all these cases we devise the upload and download to 50% and 50%.
So if the server is not sending any data then it will jump from 50 to 100 directly"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900