HTML5 JS FileReader Interface

Source: Internet
Author: User

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

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.