ajax ----进度条的原理
一、进度条的原理
新知识点: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. |
ajax ----进度条的原理的更多相关文章
- 基于Blod的ajax进度条下载实现
普通的浏览器下载 在web开发中,如果要实现下载功能,往往都是使用新开web页面或者是使用iframe的形式.实现起来其实很简单: <a target="_blank" hr ...
- OK335xS psplash 进度条工作原理 hacking
#!/bin/sh # # rc This file is responsible for starting/stopping # services when the runlevel changes ...
- ajax进度条
.graph { width: 400px; height: 25px; border: 1px solid #f8b3d0; } .bar { background-color: #ffe7f4; ...
- HTML5-svg圆形饼状图进度条实现原理
<svg width="440" height="440" viewbox="0 0 440 440"> <circle ...
- 关于 webapi ajax进度条信息设置
1.Web.config 设置跨域 <httpProtocol> <customHeaders> <add name="Access-Control-Allow ...
- atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7
atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7 1. 实现原理 1 2. 大的文件上传原理::使用applet 1 3. 新的bp 2 1. 性能提升---分割小文件上传 ...
- jQuery ajax 标准写法及进度条绘制
jQuery ajax 标准写法及进度条绘制 $.ajax({ url: "http://www.microsoft.com", //请求的url地址 dataType: &quo ...
- php实现进度条原理
PHP实现进度条的原理: 模版替换,在页面设置一个标识,轮子自己的页面,不发请求给服务器,由服务器端获得进度,然后替换该页面标识,达到进度条效果. 页面代码: 1 2 3 4 5 6 7 8 9 10 ...
- PHP上传实现进度条
Web上传文件的三种解决方案
随机推荐
- MVC View显示详解(RenderBody,RenderPage,RenderSection,Partial)
一.Views文件夹 -> Shared文件夹下的 _Layout.cshtml 母版页 @RenderBody 当创建基于_Layout.cshtml布局页面的视图时,视图的内容会和布局页面合 ...
- CocoaPods 2016最新安装和使用说明
cocoapods 简介: CocoaPods是OS X和iOS下的一个第三类库管理工具,通过CocoaPods工具我们可以为项目添加被称为“Pods”的依赖库(这些类库必须是CocoaPods本身所 ...
- java udp (使用类调用双通信)1
项目需要就使用了UDP通信,做了java的双方通信,其实代码还是来自之前的udp学习代码,自己加了注释,并且优化的使用类来封装关于通信类库的使用代码 目的是为了在安卓项目中使用时,可以通过实例化,调用 ...
- java.math.BigDecimal()的用法
Java中简单的浮点数类型float和double是不能进行运算的,不光Java,很多语言都是这样. 我们运行下面程序你将会看到 public class TestMathDecimal { publ ...
- Spring BeanFactory实例化Bean的详细过程
Spring中Bean的实例化是Bean生命周期的一个重要环节,通常Bean初始化后将不再改变. 那么Spring实例Bean的过程到底是怎么样的呢?! 要想获取到一个bean对象,得先通过Bea ...
- Trick 小记
1.\[P(A|BC) = \frac{P(AB|C)}{P(B|C)}\] 2. In EM algorithm, the usual expectation function can be wri ...
- C++/C语言程序代码
//-----------------------------------1 #include <stdio.h> #include<stdlib.h> void main() ...
- Java日常总结之LinkedList、ArrayList的效率分析
前言: 在我们平常开发中难免会用到List集合来存储数据,一般都会选择ArrayList和LinkedList,以前只是大致知道ArrayList查询效率高LinkedList插入删除效率高,今天来实 ...
- 未来工厂——电器行业ERP案例
江苏科兴电器有限公司位于全国著名的“银杏之乡”泰兴市南首,主要生产35kV及以下电流.电压互感器等系列产品.产品多次经国家及省市技术监督部门抽检合格,广泛应用于国家重点工程.“COSINE”商标荣获泰 ...
- 《深入理解Java虚拟机》学习笔记之字节码执行引擎
Java虚拟机的执行引擎不管是解释执行还是编译执行,根据概念模型都具有统一的外观:输入的是字节码文件,处理过程是字节码解析的等效过程,输出的是执行结果. 运行时栈帧结构 栈帧(Stack Frame) ...