ajax上傳進度條

<script type="text/javascript">
  function register(){
    var frm = document.getElementById('frm');
    var fd = new FormData(frm);
    var request = new XmlHttpRequest();
    request.open('post', './demo.php');
    request.onreadystatechange = function(){
      if (request.readystate == 4 && request.status == 200) {
          if(request.responseText == '1'){
            alert('上傳成功!');
          }
      }
      request.send(fd);
      //上傳進度條
      request.upload.onprogress = function(e){
        var total = e.total;//總大小
        var loaded = e.loaded; //已上傳的大小
        var percent = Math.floor(loaded/total*100);
        document.getElementById('in').style.width = percent+'%';
        document.getElementById('in').innerHTML = percent+'%';javascript

      }
    }
  }
</script>
</head>
<body>
<form id="frm">
  用戶名:<input type="text" name="username"><br>
  頭像:<input type="file" name="face"><br>
  <div id="out">
    <div id="in"></div>
  </div>
  <input type="button" value="上傳" id="btn">
</form>
</body>php

相關文章
相關標籤/搜索