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 access the file system asynchronously in the main thread of the browser, reading the data in the file
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 |
The method does not return a read result, regardless of the success or failure of the read, which is stored in the result property.
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 |
Use of the FileReader interface
- <p>
- <label> Please select a file:</label>
- <input type="file" id="file" />
- <input type="button" value="read image" onclick="Readasdataurl ()" />//Read image
- <input type="button" value="read binary data" onclick="readasbinarystring ()" />//Read binary data
- <input type="button" value="read text file" onclick="Readastext ()" />//Read text file
- </P>
- <div id="result" name="result"></div>
- var result=document.getElementById ("result");
- var file=document.getElementById ("file");
- Determine if the browser supports the FileReader interface
- if (typeof FileReader = = ' undefined ') {
- result. innerhtml="<div> Browser does not support FileReader interface! </div> ";
- Make the selection control not operational
- File.setattribute ("Disabled", "disabled");
- }
- function Readasdataurl () {
- Verify that the image file is
- var file = document.getElementById ("file"). Files[0];
- if (!/image\/\w+/.test (File.type)) {
- Alert ("See Clearly, this needs a picture!") ");
- return false;
- }
- var reader = new FileReader ();
- Read the file into the page as a data URL
- Reader.readasdataurl (file);
- reader.onload=function (e) {
- var result=document.getElementById ("result");
- Show Files
- result.innerhtml=' alt="" /> ";
- }
- }
- function readasbinarystring () {
- var file = document.getElementById ("file"). Files[0];
- var reader = new FileReader ();
- Read the file into the page in binary form
- reader.readasbinarystring (file);
- reader.onload=function (f) {
- var result=document.getElementById ("result");
- Show Files
- result.innerhtml=This.result;
- }
- }
- function Readastext () {
- var file = document.getElementById ("file"). Files[0];
- var reader = new FileReader ();
- Read the file into the page as text
- Reader.readastext (file);
- reader.onload=function (f) {
- var result=document.getElementById ("result");
- Show Files
- result.innerhtml=This.result;
- }
- }
- </Script>
Technical-filereader interface used in graduation thesis