How to use FileReader interface in parsing HTML5

Source: Internet
Author: User
FileReader interface provides an asynchronous API, using the API can be in the main thread of the browser to access the file system, read the data in the file, the following through this article to share with you HTML5 FileReader interface usage examples, interested friends to see together

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> <p I D= "Result" name= "result" ></p>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.