Description
CDN Search
75CDN BootCDN
<!DOCTYPE html>
<html>

<head>
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<meta charset="utf-8" />
	<meta name="referrer" content="never">
	<title>html5上传文件及上传真实进度</title>
</head>

<body>
	<h2>html5上传文件及上传真实进度</h2>
	<p>这是是浏览器上传的进度,与后台无关,就算后台拒绝也没关系</p>
	<hr/>
	<input type="file" id="file">
	<p id="per"></p>
</body>

</html>
document.getElementById('file').onchange = function () {
    if (this.files.length) {
        var file = this.files[0];

        var fd = new FormData();
        fd.append('file', file);

        //上传
        var xhr = new XMLHttpRequest();
        xhr.upload.onprogress = function (event) {
            if (event.lengthComputable) {
                //上传百分比
                var per = (event.loaded / event.total) * 100;
                per = per.toFixed(2) + " %";
                document.getElementById('per').innerHTML = per;
                console.log(per);
            }
        };

        xhr.open("post", "//upload.qiniu.com/", true);
        xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
        xhr.send(fd);
        xhr.onreadystatechange = function (e) {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    console.log(xhr.responseText)
                } else {
                    console.log('上传失败');
                }
            }
        }
    }
}