1. FileReader Overview
FileReader object allows a web application to asynchronously read the contents of a file (or raw data buffer) stored on the user's computer, using File
or Blob
object specifies the file or data to be read.
The File object can be a FileList
object returned after the user selects a file on an <input>
element, or it can be generated by a drag-and-drop operation. The DataTransfer
object can also be the result returned after executing the
mozGetAsFile() method on a HTMLCanvasElement
.
2. FileReader interface method
Method name | Parameters | Description |
##readAsBinaryString | file | Read the file as binary code |
readAsText | file,[encoding] | Read file as text |
readAsDataURL | file | Read file as DataURL |
readAsArrayBuffer | file | Read the file as an ArrayBuffer object
|
abort | (none) | Abort read operation |
event | Description |
Triggered when data reading is interrupted | |
Triggered when data reading error occurs | |
Triggered when data reading starts | |
Data reading | |
Triggered when data reading is successfully completed | |
Triggered when data reading is completed, regardless of success or failure |
4. Usage examples
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>Html5Test</title> </head> <body> <article> <header></header> <section> <p> <lable>请选择一个文件:</lable> <input type="file" id="file_reader"> <input type="button" value="读取图像" onclick="readAsDataUrl();"> <input type="button" value="读取二进制数据" onclick="readAsBinaryString();"> <input type="button" value="读取文本文件" onclick="readAsText();"> </p> <p id="file_reader_result" name="file_reader_result"> <!-- 这里用来显示读取结果 --> </p> <script> var file_reader_result = document.getElementById("file_reader_result"); // 检测浏览器是否支持FileReader if (typeof FileReader == "undefined") { file_reader_result.innerHTML = "您的浏览器不支持FileReader"; file.setAttribute('disabled', 'disabled'); } // 将文件以Data Url形式读入页面 function readAsDataUrl(){ // 检查是否为图像文件 var f = document.getElementById("file_reader").files[0]; if (!/image\/\w+/.test(f.type)) { file_reader_result.innerHTML = "请上传图片文件!"; return false; }; var reader = new FileReader(); // 将文件以Data Url形式读入页面 reader.readAsDataURL(f); reader.onload = function(e){ file_reader_result.innerHTML = '<img src="/static/imghwm/default1.png" data-src="' + this.result + '" class="lazy" alt="图片" />'; } } // 将文件以二进制形式读入页面 function readAsBinaryString(){ // 检查是否为图像文件 var f = document.getElementById("file_reader").files[0]; if (!/image\/\w+/.test(f.type)) { file_reader_result.innerHTML = "请上传图片文件!"; return false; }; var reader = new FileReader(); // 将文件以二进制形式读入页面 reader.readAsBinaryString(f); reader.onload = function(e){ file_reader_result.innerHTML = this.result; } } // 将文件以文本形式读入页面 目前测试仅支持txt文件 function readAsText(){ // 检查是否是文本文件 var f = document.getElementById("file_reader").files[0]; if (!/text\/\w+/.test(f.type)) { file_reader_result.innerHTML = "请上传文本文件!"; return false; }; var reader = new FileReader(); // 将文件以文本形式读入页面 reader.readAsText(f); reader.onload = function(e){ file_reader_result.innerHTML = this.result; } } </script> </section> </article> </body> </html>
The above is the detailed content of Detailed introduction to HTML5 FileReader interface. For more information, please follow other related articles on the PHP Chinese website!

MicrodatainHTML5enhancesSEOanduserexperiencebyprovidingstructureddatatosearchengines.1)Useitemscope,itemtype,anditempropattributestomarkupcontentlikeproductsorevents.2)TestmicrodatawithtoolslikeGoogle'sStructuredDataTestingTool.3)ConsiderusingJSON-LD

HTML5introducesnewinputtypesthatenhanceuserexperience,simplifydevelopment,andimproveaccessibility.1)automaticallyvalidatesemailformat.2)optimizesformobilewithanumerickeypad.3)andsimplifydateandtimeinputs,reducingtheneedforcustomsolutions.

H5 is HTML5, the fifth version of HTML. HTML5 improves the expressiveness and interactivity of web pages, introduces new features such as semantic tags, multimedia support, offline storage and Canvas drawing, and promotes the development of Web technology.

Accessibility and compliance with network standards are essential to the website. 1) Accessibility ensures that all users have equal access to the website, 2) Network standards follow to improve accessibility and consistency of the website, 3) Accessibility requires the use of semantic HTML, keyboard navigation, color contrast and alternative text, 4) Following these principles is not only a moral and legal requirement, but also amplifying user base.

The H5 tag in HTML is a fifth-level title that is used to tag smaller titles or sub-titles. 1) The H5 tag helps refine content hierarchy and improve readability and SEO. 2) Combined with CSS, you can customize the style to enhance the visual effect. 3) Use H5 tags reasonably to avoid abuse and ensure the logical content structure.

The methods of building a website in HTML5 include: 1. Use semantic tags to define the web page structure, such as, , etc.; 2. Embed multimedia content, use and tags; 3. Apply advanced functions such as form verification and local storage. Through these steps, you can create a modern web page with clear structure and rich features.

A reasonable H5 code structure allows the page to stand out among a lot of content. 1) Use semantic labels such as, etc. to organize content to make the structure clear. 2) Control the rendering effect of pages on different devices through CSS layout such as Flexbox or Grid. 3) Implement responsive design to ensure that the page adapts to different screen sizes.

The main differences between HTML5 (H5) and older versions of HTML include: 1) H5 introduces semantic tags, 2) supports multimedia content, and 3) provides offline storage functions. H5 enhances the functionality and expressiveness of web pages through new tags and APIs, such as and tags, improving user experience and SEO effects, but need to pay attention to compatibility issues.


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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
