FileReader对象采用异步方式读取文件,在不同的读取阶段会触发不同的事件。

事件列表:

(1).abort事件:读取中断或调用reader.abort()方法时触发。

(2).error事件:读取出错时触发。

(3).load事件:读取成功后触发。

(4).loadend事件:读取完成后触发,不管是否成功。触发顺序排在 onload 或 onerror 后面。

(5).loadstart事件:读取将要开始时触发。

(6).progress事件:读取过程中周期性触发。

代码实例:

[HTML] 纯文本查看 复制代码运行代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>蚂蚁部落</title>
<script>
var h = {
  init: function () {
    var _this = this;
 
    document.getElementById("File").onchange = _this.fileHandler;
    document.getElementById("Abort").onclick = _this.abortHandler;
 
    _this.status = document.getElementById("Status");
    _this.progress = document.getElementById("Progress");
    _this.percent = document.getElementById("Percent");
 
    _this.loaded = 0;
    //每次读取1M 
    _this.step = 1024 * 1024;
    _this.times = 0;
  },
  fileHandler: function (e) {
    var _this = h;
    _this.file = this.files[0];
    _this.reader = new FileReader();
 
    _this.total = _this.file.size;
 
    _this.reader.onloadstart = _this.onLoadStart;
    _this.reader.onprogress = _this.onProgress;
    _this.reader.onabort = _this.onAbort;
    _this.reader.onerror = _this.onerror;
    _this.reader.onload = _this.onLoad;
    _this.reader.onloadend = _this.onLoadEnd;
    //读取第一块 
    _this.readBlob(0);
  },
   
  readBlob: function (start) {
    var _this = h;
 
    var blob,
        file = _this.file;
 
    _this.times += 1;
 
    blob = file.slice(start, start + _this.step + 1);
    _this.reader.readAsText(blob);
  },
 
  onLoadStart: function () {
    console.log("读取将要开始");
    var _this = h;
  },
 
  onProgress: function (e) {
    console.log("正在进行读取");
    var _this = h;
    _this.loaded += e.loaded;
    //更新进度条 
    _this.progress.value = (_this.loaded / _this.total) * 100;
  },
 
  onAbort: function () {
    console.log("读取中断");
    var _this = h;
  },
  onError: function () {
    console.log("读取中断");
    var _this = h;
  },
 
  onLoad: function () {
    console.log("读取完成");
    var _this = h;
 
    if (_this.loaded < _this.total) {
      _this.readBlob(_this.loaded);
    } else {
      _this.loaded = _this.total;
    }
  },
 
  onLoadEnd: function () {
    console.log("读取结束");
    var _this = h;
  },
   
  abortHandler: function () {
    var _this = h;
 
    if (_this.reader) {
      _this.reader.abort();
    }
  }
};
 
window.onload = function () {
  h.init();
}
</script>
</head>
<body>
<form>
  <fieldset>
    <legend>分度读取文件:</legend>
    <input type="file" id="File" />
    <input type="button" value="中断" id="Abort" />
    <p>
      <label>读取进度:</label>
      <progress id="Progress" value="0" max="100"></progress>
    </p>
    <p id="Status"></p>
  </fieldset>
</form
</body>
</html>

上面是一个完整的演示代码,读取较大的文件演示效果更佳明显,文件是分段上传的。

控制台截图如下:

出处:http://www.softwhy.com/article-8816-1.html

http://www.softwhy.com/article-9805-1.html

最新文章

  1. Linux 计划任务 访问网页
  2. 如何使用github搭建个人博客
  3. C#操作SQLite数据库
  4. SQL 常用函数
  5. 如何向非技术人(程序猿)解释SQL注入?
  6. JS学习之DOM节点的关系属性封装、克隆节点、Dom中Style常用的一些属性等小结
  7. uistepper on ios versions prior to 5.0
  8. C#函数式编程-序列
  9. k8s 创建资源的两种方式 - 每天5分钟玩转 Docker 容器技术(124)
  10. ngx.re.match使用示例
  11. ELK-ElasticSearch索引详解
  12. IntelliJ IDEA插件系列
  13. IP 地址基本知识
  14. A - 畅通工程续 最短路
  15. Linux文本处理工具——Sed
  16. Redis学习笔记9--Redis持久化
  17. Oracle数据库查看SID和service_name
  18. python unittest单元测试框架-3用例执行顺序、多级目录、装饰器、fixtures
  19. [转]WebForm中使用MVC
  20. Linux Shell常用技巧(五)

热门文章

  1. Spring Events
  2. @RequestMapping 用法详解
  3. Proxy代理对象是如何调用invoke()方法的.
  4. BST平衡二叉树的后继结点(最近的大)
  5. Linux内核引用计数器kref结构
  6. 内网服务器离线编译安装mysql5.7并调优
  7. 67 GC 垃圾回收机制
  8. C语言向txt文件写入当前系统时间(Log)
  9. java 简单工具
  10. k8s-架构中各个组件介绍