一、进度条的原理

新知识点:Html5中FormData,xmlHttpRequest中的upload属性,progress事件监控

xmlHttpRequest中的upload属性,实现:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="js/jquery-1.9.1.js"></script> </head>
<body>
<form action="#" id="form" method="post" enctype="multipart/form-data">
<input type="text" name="password"><br>
<input type="file" id="file" name="apk_file" class="file"><br>
</form>
<input type="button" value="ajax" id="ajax"> </body>
<script>
window.onload=function(){ document.getElementById('ajax').addEventListener('click',function(){ var formElement = document.getElementById("form");
var xhr=getXhr();
console.log(xhr.progress,xhr.upload)
var data=new FormData(formElement)
//
// console.log(xhr.progress,xhr.upload)
// 判断浏览器是否有xhr.upload属性
if (xhr.upload) {
//开始
xhr.upload.onloadstart =function(e){
console.log(e,'start开始')
} //发送
xhr.upload.onprogress = function (e) {
var done = e.position || e.loaded, total = e.totalSize || e.total;
console.log('xhr.upload.onprogress: ' + done + ' / ' + total + ' = ' + (Math.floor(done / total * 1000) / 10) + '%,onprogress. ');
}; //结束 事件 loadend:在通信完成或者触发error、abort或load事件后触发。 4种 不同的事件
//成功返回调用方法
xhr.upload.onload =function(e){
console.log('onloadend')
}
//错误返回调用方法
xhr.upload.onerror =function(e){
console.log('onerror')
} xhr.upload.onloadend =function(e){
console.log('onloadendend')
} //发送完成 请求状态监控
xhr.onreadystatechange = function (e) {
if (4 == this.readyState) {
console.log('xhr upload complete',e,'onreadystatechange状态为4的时候');
}
};
}
xhr.open("POST", "01.php");
xhr.send(data);
})
} // 定义创建XMLHttpRequest对象的函数
function getXhr(){
// 声明XMLHttpRequest对象
var xhr;
// 根据不同浏览器创建
if(window.XMLHttpRequest){
// 其他浏览器
xhr = new XMLHttpRequest();
}else{
// IE浏览器(8及之前)
xhr = new ActiveXObject("Microsoft.XMLHttp");
}
// 返回XMLHttpRequest对象
return xhr;
}
</script>
</html>
 

xmlhtmlrequest.upload属性下面的方法有: 来源

Event listeners Data type of response property
onloadstart The fetch starts
onprogress Data transfer is going on
onabort The fetch operation was aborted
onerror The fetch failed
onload The fetch succeeded
ontimeout The fetch operation didn't complete by the timeout the author specified
onloadend The fetch operation completed (either success or failure)

通过progress事件,实现:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="js/jquery-1.9.1.js"></script> </head>
<body>
<form action="#" id="form" method="post" enctype="multipart/form-data">
<input type="text" name="password"><br>
<input type="file" id="file" name="apk_file" class="file"><br>
</form>
<input type="button" value="ajax" id="ajax"> </body>
<script> document.getElementById('file').addEventListener('change', function (e) {
var xhr = getXhr();
// 通过formData 获得参数 this.File
var data=new FormData(document.getElementById("form"))
// 监控 progress事件
xhr.addEventListener('progress', function (e) {
var done = e.position || e.loaded, total = e.totalSize || e.total;
console.log('xhr progress: ' + (Math.floor(done / total * 1000) / 10) + '%');
}, false); xhr.onreadystatechange = function (e) {
if (4 == this.readyState) {
console.log(['xhr upload complete', e]);
}
};
xhr.open('post', '01.php', true);///这里写上url~
xhr.send(data);
}, false); function getXhr(){
// 声明XMLHttpRequest对象
var xhr;
// 根据不同浏览器创建
if(window.XMLHttpRequest){
// 其他浏览器
xhr = new XMLHttpRequest();
}else{
// IE浏览器(8及之前)
xhr = new ActiveXObject("Microsoft.XMLHttp");
}
// 返回XMLHttpRequest对象
return xhr;
}
</script>
</html>

https://developer.mozilla.org/zh-CN/docs/Web/Events/%E8%BF%9B%E5%BA%A6%E6%9D%A1

Property Type Description
target 只读 EventTarget The event target (the topmost target in the DOM tree).
type 只读 DOMString The type of event.
bubbles 只读 Boolean Whether the event normally bubbles or not
cancelable 只读 Boolean Whether the event is cancellable or not?
lengthComputable boolean Specifies whether or not the total size of the transfer is known. Read only.
loaded unsigned long (long) The number of bytes transferred since the beginning of the operation. This doesn't include headers and other overhead, but only the content itself. Read only.
total unsigned long (long) The total number of bytes of content that will be transferred during the operation. If the total size is unknown, this value is zero. Read only.

最新文章

  1. touch — 设定文件的访问和修改时间
  2. zpf 获取表单等数据的用法
  3. Porlet标准:JSR168/JSR286/WSRP(转载)
  4. TFS服务器(微软源代码管理服务器)上彻底删除项目
  5. MVC——数据库增删改查(aspx)
  6. 【转】 学习ios(必看经典)牛人40天精通iOS开发的学习方法【2015.12.2
  7. c#字符串方法
  8. Android 基本控件
  9. 全网最全ASP.NET MVC 教程汇总
  10. 软件及博客的markdown支持度的评测
  11. C#面试考点集锦
  12. socketServer并发处理socket
  13. HBuilder设置沉浸式状态栏显示效果
  14. 分享一个生成反遗忘复习计划的java程序
  15. springmvc返回中文乱码问题
  16. web网站的并发量级别
  17. poj2492 A Bug's Life【并查集】
  18. OpenACC 书上的范例代码(Jacobi 迭代),part 1
  19. openwrt,mjpeg流,wifi摄像头与APP联动,拍照、录像
  20. Docker for Windows(三)Docker镜像与容器的区别&amp;常用命令

热门文章

  1. 使用devstack搭建openstack Newton 版本的坑
  2. 爬虫:把廖雪峰的教程转换成 PDF 电子书
  3. android学习19--Matrix.mapPoints作用
  4. CSS 3 过渡效果之jquery 的fadeIn ,fadeOut
  5. 全文搜索 Contains 与like 的区别
  6. angularjs+ionic注册页面表单验证(手机号、确认密码、60s后重发验证码)
  7. Django|第一部
  8. 解决CenOS 7下启动ActiveMQ时报错
  9. java程序员入门:英语好不好对编程到底有没有影响
  10. Java IO之File和IO