Tracking upload progress

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    Tracking upload progress

    I've written a program that uploads a file via FTP to my server. It allows you to select the file you want to upload, type the path to upload it to, then upload it. While it's uploading it uses a loop to keep track of the progress in this format:

    bytes uploaded / total bytes

    I'm trying to also have it display the percent done (something I thought would be super simple) by having a double called percentDone. Inside the while loop I do this:

    percentDone = (int)(totalUplo aded / finfo.Length) * 100;

    And then the line that prints is:

    textbox1.Text = "Uploaded " + totalUploaded + "/" + finfo.Length + " bytes - (" + percentDone + "%)";

    Problem is while I'm uploading the percent stays at 0 until the upload is complete, then it jumps to 100%. I've got Application.DoE vents(); inside my while loop, so the textbox text DOES update the uploaded / total part, just not the percent.

    Am I doing something wrong? I've tried taking a screenshot of the program while it was uploading and dividing the bytes uploaded by the total bytes, I used these numbers:

    657408 / 6598144

    Which should give me 0.09963...multi plied by 100 gives me 9.96%. I just want to floor the result, so I'd think that Math.Floor(perc entDone) would work. Problem is, like I said, percentDone just stays at 0 for the whole time until the upload is complete.

    Please help me out with this, what the heck am I doing wrong?
  • cloud255
    Recognized Expert Contributor
    • Jun 2008
    • 427

    #2
    Hi,

    Could you post your full code please.
    Check the background worker class which contains DoWork and ProgressChanged members. The DoWork class will allow you to perform the download in the background on a separate thread while the ProgressChanged will allow you to update the download progress in real time.

    Comment

    • HaLo2FrEeEk
      Contributor
      • Feb 2007
      • 404

      #3
      I've been advised before to NOT use the DoWork class. I can't really post the full code because it's really long, and it's an upload, not a download.

      Regardless, I figured it out. Apparently dividing an int by another int will only return an int, meaning I was only getting 0 and 1 (multipled by 100 was 0 and 100.) All I had to do was make one of the numbers a double, that was easy, I just cast the totalDownloaded as a double and it worked. All I had to do was round the number to 2 decimal points and I'm golden.

      Comment

      Working...