Renderings
The old rules on the first renderings
First tell us about some of the ways in FileReader H5 and events
FileReader method
name
effect
about()
Read termination
readAsBinaryString(file)
The file is read as a binary encoding
readAsDataURL(file)
The document read as DataURL coding
readAsText(file, [encoding])
Will read a text file
readAsArrayBuffer(file)
The document read as arraybuffer
FileReader events
name
effect
onloadstart
When the trigger to start reading
onprogress
Loading
onloadend
Read completion trigger, regardless of success or failure
onload
When the file is read successfully completed trigger
Onboart
When an interrupt is triggered
onerror
Error trigger
Code
The core idea of the distribution of the file to read the file read per block M.
HTML part
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
<html lang ="en" > <head > <meta charset ="UTF-8" > <title > Document</title > </head > <body > <form > <fieldset > <legend > 分步读取文件:</legend > <input type ="file" id ="File" > <input type ="button" value ="中断" id ="Abort" > <p > <lable > 读取进度:</lable > <progress id ="Progress" value ="0" max ="100" > </progress > </p > </fieldset > </form > <script src ="./loadFile.js" > </script > <script > var progress = document .getElementById('Progress' ); var events = { load: function ( ) { console .log('loaded' ); }, progress: function (percent ) { console .log(percent); progress.value = percent; }, success: function ( ) { console .log('success' ); } }; var loader; document .getElementById('File' ).onchange = function (e ) { var file = this .files[0 ]; console .log(file) loader = new FileLoader(file, events); }; 大专栏 HTML5 FileReader分布读取文件以及其方法简介 r/> document .getElementById('Abort' ).onclick = function ( ) { loader.abort(); } </script > </body > </html >
loadFile.js部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
var FileLoader = function (file, events ) { this .reader = new FileReader(); this .file = file; this .loaded = 0 ; this .total = file.size; this .step = 1024 * 1024 ; this .events = events || {}; this .readBlob(0 ); this .bindEvent(); } FileLoader.prototype = { bindEvent: function (events ) { var _this = this , reader = this .reader; reader.onload = function (e ) { _this.onLoad(); }; reader.onprogress = function (e ) { _this.onProgress(e.loaded); }; }, onProgress: function (loaded ) { var percent, handler = this .events.progress; this .loaded += loaded; percent = (this .loaded / this .total) * 100 ; handler && handler(percent); }, onLoad: function ( ) { var handler = this .events.load; handler && handler(this .reader.result); if (this .loaded < this .total) { this .readBlob(this .loaded); } else { this .loaded = this .total; this .events.success && this .events.success(); } }, readBlob: function (start ) { var blob, file = this .file; IF (file.slice) { BLOB = file.slice (Start, + Start the this .step); } the else { BLOB = File; } the this .reader.readAsText (BLOB); }, ABORT: function ( ) { var reader = this .reader; if (reader) { reader.abort(); } } }