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!