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!

HTML5 is a key technology for building modern web pages, providing many new elements and features. 1. HTML5 introduces semantic elements such as, , etc., which enhances web page structure and SEO. 2. Support multimedia elements and embed media without plug-ins. 3. Forms enhance new input types and verification properties, simplifying the verification process. 4. Offer offline and local storage functions to improve web page performance and user experience.

Best practices for H5 code include: 1. Use correct DOCTYPE declarations and character encoding; 2. Use semantic tags; 3. Reduce HTTP requests; 4. Use asynchronous loading; 5. Optimize images. These practices can improve the efficiency, maintainability and user experience of web pages.

Web standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.

H5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.

H5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

HTML5hassignificantlytransformedwebdevelopmentbyintroducingsemanticelements,enhancingmultimediasupport,andimprovingperformance.1)ItmadewebsitesmoreaccessibleandSEO-friendlywithsemanticelementslike,,and.2)HTML5introducednativeandtags,eliminatingthenee


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development 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.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment