How to get progress from XMLHttpRequest

前端 未结 8 1910
借酒劲吻你
借酒劲吻你 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:32

    The only way to do that with pure javascript is to implement some kind of polling mechanism. You will need to send ajax requests at fixed intervals (each 5 seconds for example) to get the number of bytes received by the server.

    A more efficient way would be to use flash. The flex component FileReference dispatchs periodically a 'progress' event holding the number of bytes already uploaded. If you need to stick with javascript, bridges are available between actionscript and javascript. The good news is that this work has been already done for you :)

    swfupload

    This library allows to register a javascript handler on the flash progress event.

    This solution has the hudge advantage of not requiring aditionnal resources on the server side.

提交回复
热议问题