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
<script type= "Text/javascript" > var result=document.getelementbyid ("result"); var File=document.getelementbyid ("file"); Determine if the browser supports the FileReader interface if (typeof FileReader = = ' undefined ') {result. Innerhtml= "<p> your browser does not support FileReader interface! </p> "; Make the selection control non-operable file.setattribute ("disabled", "disabled"); } function Readasdataurl () {//check whether the image file is var, filename = document.getElementById ("file"). Files[0]; if (!/image\/\w+/.test (File.type)) {alert ("See Clearly, this requires a picture! "); return false; } var reader = new FileReader (); Reads the file into the page reader.readasdataurl (file) in the form of the data URL; Reader.onload=function (e) {var result=document.getelementbyid ("result"); Show Files result.innerhtml= ' "; }} function Readasbinarystring () {var file = document.getElementById ("file"). Files[0]; var reader = new FileReader (); Reads the file in binary form into the page reader.readasbinarystring (file); Reader.onload=function (f) {var Result=document.getelementbyid ("result"); Display file Result.innerhtml=this.result; }} function Readastext () {var file = document.getElementById ("file"). Files[0]; var reader = new FileReader (); Reads the file into the page reader.readastext (file) in text form; Reader.onload=function (f) {var Result=document.getelementbyid ("result"); Display file Result.innerhtml=this.result; }} </script> <p> <label> Please select a file:</label> <input type= "file" id= "file"/> <input type= "button" value= "read image" onclick= "Readasdataurl ()"/> <input type= "button" value= "read binary data" onclick= "Readasbinarystring ()"/> <input type= "button" value= "read text file" onclick= "Readastext ()"/> </p> <div Id= "Result" name= "result" ></div>
HTML5 JS FileReader Interface (art fair, add items, upload pictures, addgoods.html page)