Posted in Javascript onApril 07, 2017
本文实例讲述了JS+HTML5 FileReader对象用法。分享给大家供大家参考,具体如下:
FileReader 实例中有四个方法来读取文件数据
1、readAsBinaryString(Blob|File)
2、readAsDataURL(Blob|File)
3、readAsText(Blob|File)
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程序设计有所帮助。
JS+HTML5 FileReader对象用法示例
- Author -
风之约声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@