2012-12-29 72 views
0

我有一個javascript腳本,我得到一個文件一樣,一個Ajax請求:Ajax請求監控流程

$.ajax({ 
     type: "GET", 
     url: "./" + img_type + ".bmp", 
     dataType: "html", 
    timeout: test_timeout, 
     cache: false, 
     success: function(msg) 
     { 
     //some stuff 
     } 
    }); 

代碼本身是正確的,完美的作品。 有沒有一種方法可以知道當請求還在進行時我已經下載了多少文件? 我的意思是,一旦請求給了我成功的消息,我知道我已經下載了整個文件,但是如果我想在開始兩秒後知道怎麼辦? 謝謝!

+0

你需要使用原來的XMLHttpRequest來做到這一點,只有一些瀏覽器支持它(Chrome,火狐,Safari,IE10) – Licson

+0

這不是一個問題,你能舉個例子嗎? – user1903898

回答

0

下面是一個例子:

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(); 
+0

這是完美的!謝謝!! – user1903898