JS+HTML5 FileReader对象用法示例
本文实例讲述了JS+HTML5 FileReader对象用法。分享给大家供大家参考,具体如下:
FileReader 实例中有四个方法来读取文件数据
1、readAsBinaryString(Blob|File)<br>
2、readAsDataURL(Blob|File)<br>
3、readAsText(Blob|File)<br>
4、readAsArrayBuffer(Blob|File)
FileReader 实例包含很多事件(onerror, onprogress, onload[result])
<input name="file" id="uploadFile" />
var file = document.getElementById("uploadFile"); var bufferSize = 1024; var pos = 0; var onload = function(e) { console.log("Read", e.target.result); var img = document.createElement("img"); img.src = e.target.result; img.width = 300; img.height = 300; document.body.appendChild(img); }; var onerror = function() {} var onprogress = function(e) {} file.onchange = function() { if (file.files) file = file.files[0]; while (pos < file.size) { var reader = new FileReader(); reader.onload = onload; reader.onerror = onerror; /*reader.readAsText(file.slice(pos, bufferSize)); pos += bufferSize; */ reader.readAsDataURL(file); pos = file.size + 1; } }
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript扩展技巧总结》、《JavaScript文件与目录操作技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。
相关推荐
northwindx 2020-05-31
Chydar 2019-07-01
凉白开 2020-07-19
xdyangxiaoromg 2020-03-27
songfens 2019-12-14
VICHOUFA 2019-09-07
skyGAYD 2019-04-29
xuanbai 2016-09-05
htmlgood 2016-06-09
markshuai 2016-05-07
yaoliuwei 2015-12-08
DreamPig 2019-06-27
PioneerFan 2015-04-03
zjnig的信息仓库 2014-01-24
CoreyJia 2013-09-21
laxexue 2019-06-25
taowanyy 2013-05-09
renpinghao 2019-06-21
yixiaof 2019-06-21