Why is my XMLHttpRequest progress console log not showing?

51 views Asked by At

The progess of the file upload is not being console logged. Why?

Here is the code I have:

const xhr = new XMLHttpRequest();
    
xhr.open('POST', apiUrl);

xhr.upload.addEventListener("progress", function(e){
    console.log(e.loaded/e.total)      
})

xhr.onload = () => {
    const response = JSON.parse(xhr.response);
    console.log(response);
    // ... do something with the successful response
};
        
xhr.send(formData);
1

There are 1 answers

1
Bo Frederiksen On BEST ANSWER

Your apiUrl response must have the HTTP response header Content-Length. If it doesn't, there is nothing to compute.

Maybe the event handler should be bound to xhr:

xhr.addEventListener("progress", function(e) {