Parsing the usage of FileReader interface in HTML5
The FileReader interface provides an asynchronous API. Using this API, you can asynchronously access the file system in the browser's main thread and read the data in the file. Here, I will share with you a detailed example of how to use the FileReader interface in HTML5 through this article. If you are interested Friends, let’s take a look.
is used to read the file into the memory and read the data in the file. The FileReader interface provides an asynchronous API, which can be used to access the file system asynchronously in the browser's main thread and read data in the file. So far, only FF3.6+ and Chrome6.0+ have implemented the FileReader interface.
1. Methods of the FileReader interface
The FileReader interface has 4 methods, 3 of which are used to read files, and the other is used to Interrupt reading. Regardless of whether the read succeeds or fails, the method does not return the read result, which is stored in the result attribute.
Methods of FileReader interface
Parameters | Description | |
---|---|---|
file | Read the file as binary encoding | |
file,[encoding] | Read file as text | |
file | Read the file as DataURL | |
(none) | Terminal read operation |
2. FileReader interface events
The FileReader interface contains a complete set of event models for capturing read files status at the time. Events of FileReader interfaceDescription | |
Interrupt | |
Error | |
Start | |
Reading | |
Success | Reading |
Reading completed, | regardless of success or failure
3. Use of FileReader interface
#
<script type="text/javascript"> var result=document.getElementById("result"); var file=document.getElementById("file"); //判断浏览器是否支持FileReader接口 if(typeof FileReader == 'undefined'){ result.InnerHTML="<p>你的浏览器不支持FileReader接口!</p>"; //使选择控件不可操作 file.setAttribute("disabled","disabled"); } function readAsDataURL(){ //检验是否为图像文件 var file = document.getElementById("file").files[0]; if(!/image\/\w+/.test(file.type)){ alert("看清楚,这个需要图片!"); return false; } var reader = new FileReader(); //将文件以Data URL形式读入页面 reader.readAsDataURL(file); reader.onload=function(e){ var result=document.getElementById("result"); //显示文件 result.innerHTML='<img src="' + this.result +'" alt="" />'; } } function readAsBinaryString(){ var file = document.getElementById("file").files[0]; var reader = new FileReader(); //将文件以二进制形式读入页面 reader.readAsBinaryString(file); reader.onload=function(f){ var result=document.getElementById("result"); //显示文件 result.innerHTML=this.result; } } function readAsText(){ var file = document.getElementById("file").files[0]; var reader = new FileReader(); //将文件以文本形式读入页面 reader.readAsText(file); reader.onload=function(f){ var result=document.getElementById("result"); //显示文件 result.innerHTML=this.result; } } </script> <p> <label>请选择一个文件:</label> <input type="file" id="file" /> <input type="button" value="读取图像" onclick="readAsDataURL()" /> <input type="button" value="读取二进制数据" onclick="readAsBinaryString()" /> <input type="button" value="读取文本文件" onclick="readAsText()" /> </p> <p id="result" name="result"></p>
The above is the detailed content of Parsing the usage of FileReader interface in HTML5. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
