有没有办法在XMLHttpRequest中下载文件/网页时获取进度信息

时间:2015-02-14 08:29:08

标签: javascript 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);

在XMLHttpRequest中下载网页时,有没有办法获取/显示进度信息?

您的评论欢迎

2 个答案:

答案 0 :(得分:0)

如果你可以使用jQuery Ajax,你可以在Ajax调用之前显示一个gif图像(它将在浏览器上自行播放),当ajax成功返回时,你可以删除gif或者进度从您的页面栏(显示:无使用CSS)。

答案 1 :(得分:0)

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

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