Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Progressbar has a value of 100% before upload progress is finished

Progressbar has a value of 100% before upload progress is finished

Scheduled Pinned Locked Moved JavaScript
helpjavascriptphpdatabasequestion
5 Posts 5 Posters 10 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Red Kipling
    wrote on last edited by
    #1

    I upload a files in my website I have this javascript code integrated to show the progress of upload But the issue is that this bar has value of 100% before the upload is completed How can i resolve this

    function uploadFile() {
    const fileInput = document.getElementById('file1');

    const file = fileInput.files\[0\];
    
    
    
    const xhr = new XMLHttpRequest();
    xhr.open('POST', 'index.php', true);
    
    xhr.upload.onprogress = function (event) {
        if (event.lengthComputable) {
            const percentComplete = (event.loaded / event.total) \* 100;
            const progress = document.getElementById('progress');
            progress.style.width = percentComplete + '%';
            progress.textContent = Math.round(percentComplete) + '%';
        }
    };
    
    xhr.onload = function () {
        if (xhr.status === 200) {
            alert('File uploaded successfully.');
        } else {
            alert('Error uploading file.');
        }
    };
    
    const formData = new FormData();
    formData.append('file1', file);
    xhr.send(formData);
    

    }

    Richard Andrew x64R J S 3 Replies Last reply
    0
    • R Red Kipling

      I upload a files in my website I have this javascript code integrated to show the progress of upload But the issue is that this bar has value of 100% before the upload is completed How can i resolve this

      function uploadFile() {
      const fileInput = document.getElementById('file1');

      const file = fileInput.files\[0\];
      
      
      
      const xhr = new XMLHttpRequest();
      xhr.open('POST', 'index.php', true);
      
      xhr.upload.onprogress = function (event) {
          if (event.lengthComputable) {
              const percentComplete = (event.loaded / event.total) \* 100;
              const progress = document.getElementById('progress');
              progress.style.width = percentComplete + '%';
              progress.textContent = Math.round(percentComplete) + '%';
          }
      };
      
      xhr.onload = function () {
          if (xhr.status === 200) {
              alert('File uploaded successfully.');
          } else {
              alert('Error uploading file.');
          }
      };
      
      const formData = new FormData();
      formData.append('file1', file);
      xhr.send(formData);
      

      }

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      My best guess is that event.loaded and event.total are integers, not floating point numbers as you expect, so it's performing integer math.

      The difficult we do right away... ...the impossible takes slightly longer.

      Richard DeemingR 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        My best guess is that event.loaded and event.total are integers, not floating point numbers as you expect, so it's performing integer math.

        The difficult we do right away... ...the impossible takes slightly longer.

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        Except that JavaScript doesn't really have a concept of "integers", unless you use the BigInt class. :)

        JavaScript language overview - JavaScript | MDN[^]:

        Number: used for all number values (integer and floating point) except for very big integers. BigInt: used for arbitrarily large integers.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        • R Red Kipling

          I upload a files in my website I have this javascript code integrated to show the progress of upload But the issue is that this bar has value of 100% before the upload is completed How can i resolve this

          function uploadFile() {
          const fileInput = document.getElementById('file1');

          const file = fileInput.files\[0\];
          
          
          
          const xhr = new XMLHttpRequest();
          xhr.open('POST', 'index.php', true);
          
          xhr.upload.onprogress = function (event) {
              if (event.lengthComputable) {
                  const percentComplete = (event.loaded / event.total) \* 100;
                  const progress = document.getElementById('progress');
                  progress.style.width = percentComplete + '%';
                  progress.textContent = Math.round(percentComplete) + '%';
              }
          };
          
          xhr.onload = function () {
              if (xhr.status === 200) {
                  alert('File uploaded successfully.');
              } else {
                  alert('Error uploading file.');
              }
          };
          
          const formData = new FormData();
          formData.append('file1', file);
          xhr.send(formData);
          

          }

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          Red Kipling wrote:

          const percentComplete = (event.loaded / event.total) * 100;

          You might try the following. Mathematically it is the same but the order of operations changes how it computes the result. const percentComplete = ((event.loaded * 100) / event.total);

          1 Reply Last reply
          0
          • R Red Kipling

            I upload a files in my website I have this javascript code integrated to show the progress of upload But the issue is that this bar has value of 100% before the upload is completed How can i resolve this

            function uploadFile() {
            const fileInput = document.getElementById('file1');

            const file = fileInput.files\[0\];
            
            
            
            const xhr = new XMLHttpRequest();
            xhr.open('POST', 'index.php', true);
            
            xhr.upload.onprogress = function (event) {
                if (event.lengthComputable) {
                    const percentComplete = (event.loaded / event.total) \* 100;
                    const progress = document.getElementById('progress');
                    progress.style.width = percentComplete + '%';
                    progress.textContent = Math.round(percentComplete) + '%';
                }
            };
            
            xhr.onload = function () {
                if (xhr.status === 200) {
                    alert('File uploaded successfully.');
                } else {
                    alert('Error uploading file.');
                }
            };
            
            const formData = new FormData();
            formData.append('file1', file);
            xhr.send(formData);
            

            }

            S Offline
            S Offline
            Steve Raw
            wrote on last edited by
            #5

            Red Kipling wrote:

            upload a files in my website I have this javascript code integrated to show the progress of upload But the issue is that this bar has value of 100% before the upload is completed How can i resolve this

            See what happens if you replace this code:

            xhr.upload.onprogress = function (event) {
            //code
            };

            ...with this

            xhr.upload.addEventListener("progress", function (event) {
            //code
            });

            Although your code is correct, try using the addEventListener method to see if that makes any difference. I believe that event listeners need to be assigned before the xhr's "open" method is called.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups