Used to read the file into memory and read the data in the file. The FileReader interface provides an asynchronous API that allows you to asynchronously access the file system in the main thread of the browser and read the data in the file. To current civilian, only ff3.6+ and chrome6.0+ implemented the FileReader interface.
1, the method of FileReader interface
The FileReader interface has 4 methods, 3 of which are used to read the file and the other to interrupt the read. The method does not return a read result, regardless of the success or failure of the read, which is stored in the result property.
Method of FileReader interface
Method Name |
Parameters |
Description |
Readasbinarystring |
File |
To read a file as a binary encoding |
Readastext |
File,[encoding] |
To read a file as text |
Readasdataurl |
File |
Read the file as Dataurl |
Abort |
(none) |
Terminal Read operations |
2. FileReader Interface Event
The FileReader interface contains a complete set of event models for capturing the state when a file is read.
Events for the FileReader interface
Event |
Describe |
Onabort |
Interrupt |
OnError |
Error |
Onloadstart |
Begin |
OnProgress |
is reading |
OnLoad |
Successfully read |
Onloadend |
Read complete regardless of successful failure |
3, the use of FileReader interface
<Scripttype= "Text/javascript"> varresult=document.getElementById ("result"); varfile=document.getElementById ("file"); //determine if the browser supports the FileReader interfaceif(typeofFileReader== 'undefined') {result. InnerHTML="<p> Your browser does not support the FileReader interface! </p>"; //make the selection control not operationalFile.setattribute ("Disabled","Disabled"); } functionReadasdataurl () {//Verify that the image file is varfile=document.getElementById ("file"). files[0]; if(!/image\/\w+/. Test (File.type)) {Alert ("look clearly, this needs a picture! "); return false; } varReader= NewFileReader (); //read the file into the page as a data URLreader.readasdataurl (file); Reader.onload=function(e) {varresult=document.getElementById ("result"); //Show Filesresult.innerhtml='' + This. Result+'"/>'; } } functionreadasbinarystring () {varfile=document.getElementById ("file"). files[0]; varReader= NewFileReader (); //read the file into the page in binary formreader.readasbinarystring (file); Reader.onload=function(f) {varresult=document.getElementById ("result"); //Show Filesresult.innerhtml= This. Result; } } functionReadastext () {varfile=document.getElementById ("file"). files[0]; varReader= NewFileReader (); //read the file into the page as textreader.readastext (file); Reader.onload=function(f) {varresult=document.getElementById ("result"); //Show Filesresult.innerhtml= This. Result; } } </Script> <P> <label>Please select a file:</label> <inputtype= "File"ID= "File" /> <inputtype= "button"value= "Read image"onclick= "Readasdataurl ()" /> <inputtype= "button"value= "Read binary data"onclick= "readasbinarystring ()" /> <inputtype= "button"value= "Read text file"onclick= "Readastext ()" /> </P> <DivID= "Result"name= "Result"></Div>
HTML5 JS FileReader Interface