XHR2:js异步上传

http://dev.opera.com/articles/xhr2/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
var xhr = new XMLHttpRequest();
 
var onProgressHandler = function(event) {
  if(event.lengthComputable) {
    var howmuch = (event.loaded / event.total) * 100;
    document.querySelector('progress').value = Math.ceil(howmuch);
  } else {
    console.log("Can't determine the size of the file.");
  }
}
 
var onLoadHandler = function() {
  displayLoadedMessage();
}
 
var onErrorHandler = function() {
  displayErrorMesssage();
}
 
xhr.upload.addEventListener('progress', onProgressHandler, false);
xhr.upload.addEventListener('load', onLoadHandler, false);
xhr.upload.addEventListener('error', onErrorHandler, false);
 
var onReadyStateHandler = function(event) {
  if( event.target.readyState == 4 && event.target.status == 200){
    
  }
}
 
xhr.open('POST','/path_to_data');
xhr.onreadystatechange = onReadyStateHandler;
xhr.send(dataToSend);


posted on 2014-05-14 22:54  dhj  阅读(176)  评论(0编辑  收藏  举报

导航