How to get progress from XMLHttpRequest

前端 未结 8 1914
借酒劲吻你
借酒劲吻你 2020-11-22 15:22

Is it possible to get the progress of an XMLHttpRequest (bytes uploaded, bytes downloaded)?

This would be useful to show a progress bar when the user is uploading a

8条回答
  •  执念已碎
    2020-11-22 15:46

    For the total uploaded there doesn't seem to be a way to handle that, but there's something similar to what you want for download. Once readyState is 3, you can periodically query responseText to get all the content downloaded so far as a String (this doesn't work in IE), up until all of it is available at which point it will transition to readyState 4. The total bytes downloaded at any given time will be equal to the total bytes in the string stored in responseText.

    For a all or nothing approach to the upload question, since you have to pass a string for upload (and it's possible to determine the total bytes of that) the total bytes sent for readyState 0 and 1 will be 0, and the total for readyState 2 will be the total bytes in the string you passed in. The total bytes both sent and received in readyState 3 and 4 will be the sum of the bytes in the original string plus the total bytes in responseText.

提交回复
热议问题