在 XMLHttpRequest 中下载 file/web 页面时有没有办法获取进度信息

Is there a way to get the progress info when download file/web page in XMLHttpRequest

下面的cpde是从yahoo.com主页下载网页

   var http = new XMLHttpRequest()
    var url = "http://yahoo.com:8080";
    var params = "";
    http.open("GET", url, true);

    // Send the proper header information along with the request
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close");

    http.onreadystatechange = function() { // Call a function when the state changes.
                if (http.readyState == 4) {
                    if (http.status == 200) {
                        console.log("ok")
                    } else {
                        console.log("error: " + http.status)
                    }
                }
            }
    http.send(params);

有没有办法get/display在XMLHttpRequest中下载网页时的进度信息?

欢迎您的评论

如果您可以使用 jQuery Ajax,您可以在 Ajax 调用之前显示一个 gif 图像(它将在浏览器中自行播放),当ajax returns 成功后,您可以从页面中删除 gif 或进度条(显示:none 使用 css)。

http.onprogress= function(){
//event.loaded and event.total usage
}

在此处详细了解 XMLHttpRequestoprogress...XHR