Javascript Ajax请求监视流
我有一个javascript脚本中的AJAX请求,其中我得到一个如下文件:Javascript Ajax请求监视流,javascript,ajax,jquery,javascript-events,Javascript,Ajax,Jquery,Javascript Events,我有一个javascript脚本中的AJAX请求,其中我得到一个如下文件: $.ajax({ type: "GET", url: "./" + img_type + ".bmp", dataType: "html", timeout: test_timeout, cache: false, success: function(msg) { //some stuff } }); 代码本身
$.ajax({
type: "GET",
url: "./" + img_type + ".bmp",
dataType: "html",
timeout: test_timeout,
cache: false,
success: function(msg)
{
//some stuff
}
});
代码本身是正确的,并且工作完美。
有没有办法知道在请求仍在进行时我下载了多少文件??
我的意思是,一旦请求给我成功消息,我知道我已经下载了整个文件,但是如果我想在开始两秒钟后知道呢??
谢谢 查看此处的“监控进度”部分:
下面是一个例子:
var xhr = new XMLHttpRequest;
xhr.onprogress = function(e){
if(e.lengthComputable){
var progress = e.position || e.loaded;
var total = e.totalSize || e.total;
var percent = progress/total*100;
//do something with progress here
}
};
xhr.onload = function(){
var content = xhr.responseText;
//do something with the result here
};
xhr.open('GET','./'+type+'.bmp',true);
xhr.send();
您需要使用原始的XMLHttpRequest来完成,并且只有一些浏览器支持它(Chrome、Firefox、Safari、IE10)。这不是问题,您可以发布一个示例吗?