How to use the H5 FileReader distribution

This article will explain in detail how to use the H5 FileReader distribution reading file, Xiaobian feels very practical, so share it with you to do a reference, I hope everyone can gain something after reading this article.

First introduce some of the methods of FileReader in the H5 and events

FileReader method

Name function

About () Termination Read

ReadasbinaryString (file) Reads files to binary encoding

ReadasDataURL (file) Reads files to DataURL encoding

readastext (file, [encoding]) Read files as text

ReadasArrayBuffer Reads files to arraybuffer

FileReader event


OnLoadStart reads start time trigger

OnProgress Read

OnLoadEnd Read Complete Trigger, no matter success or fail

OnLoad file read successfully completed

onAbort interrupt time trigger


when an error

distributes the core idea of ​​reading the file, and the file block is read in each M.

HTML part

Reading progress:

Var Progress = Document.GtelementByid ('progress "; // progress bar var events = {load: function () {console.log (' loaded ');}, progress: function (percent) {Console .log (percent); progress.value = percent;}, success: function () {console.log ('success ");}}; var loader; // Select the file to be uploaded and trigger the onchange event Document.GtelementByid ('File'). Onchange = function (e) {Var file = this.files [0]; console.log (file) //loadfile.js loadinger = new fileloader (file, events);}; document.getElementByid ('Abort'). OnClick = function () {loadinger. LoadFile.js portion / ** file reading module * File file object * Events event Return Object contains success, loading, progress * / var fileloader = function (file, events) {this.reader = new fileReader (); this.file = file; this.loaded = 0; this. Total = file.size; // Read 1M this.step = 1024 * 1024; = events || {}; // Read the first twice.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);}; // Start, Abort, Error callback temporary}, // progress event returns to onprogress: function (loading) {var percent, handler =; // progress bar this.Loaded + = loading; percent = (this .loaded / * 100; Handler && Handler (PERCENT);}, // Read End (call every time READ, not a whole) online: function () {var handler = this.Events.Load ; // should send read data handler && handler (this.Reader.Result); // If not read, continue to read if (this.loaded

H5 FileReader distribution reading fileUse it to share here, I hope that the above can have some help to everyone, you can learn more. If the article is good, you can share it out to let more people see.

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment