JS reads file FileReaderThe 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 a File or Blob object to specify the file or data to read. documentFileReader Events and methodsEvent Handling
Standard Methods
Abort the read operation. On return, the readyState property is DONE.
Start reading the contents of the specified Blob. Once completed, the result property will contain the ArrayBuffer data object of the read file.
Start reading the contents of the specified Blob. Once completed, the result property will contain a string in the format of a data: URL representing the contents of the file read.
Start reading the contents of the specified Blob. Once completed, the result property will contain a string representing the contents of the file read. Basic UseFile preparation: read.txt (you can read any file on your computer) HTML Structure <input type="file" multiple> JS call <script> window.onload = function(){ var inpFile = document.querySelector('input[type=file]') inpFile.addEventListener('change', function(){ var reader = new FileReader() // Send asynchronous request // 0. Use the readAsText method (read the result as normal text) reader.readAsText(this.files[0]) // Result of successful reading: The file has been read successfully (the file read.txt on the computer) reader.onload = function(){ //After reading, the data is saved in the result property of the object console.log(this.result)//Print: The file has been read successfully } }) } </script> JS calls use other methods (other methods are used in the same way) readAsDataURL window.onload = function(){ var inpFile = document.querySelector('input[type=file]') inpFile.addEventListener('change', function(){ var reader = new FileReader() // Use readAsDataURL (to get base64 encoding) reader.readAsDataURL(this.files[0]) reader.onload = function(){ console.log(this.result) //data:text/plain;base64,5bey57uP5oiQ5Yqf6K+75Y+W5paH5Lu2 } }) } Event HandlingJS call (still use the above HTML and files – or prepare a larger file; the effect will be better) window.onload = function(){ var inpFile = document.querySelector('input[type=file]') inpFile.addEventListener('change', function(){ var reader = new FileReader() reader.readAsText(this.files[0]) var count = 0; reader.onloadstart = function(){ console.log("onloadstart state"+this.readyState) console.log("Start loading") } reader.onloadend = function(){ console.log("onloadend state"+this.readyState) console.log("Loading completed") } reader.onprogress = function(){ count++; console.log("onprogress state"+this.readyState) console.log("Loading"+count) } reader.onload = function(){ console.log("The data obtained by onload is "+this.result) console.log("status"+this.readyState) } reader.onerror = function(){ console.log('something went wrong') } reader.onerror = function(){ console.log('Handle the abort event. This event is triggered when the read operation is interrupted.') } }) } The results are as follows Important results analysis:
Node operation file (readfile)Based on the above, we can see that JavaScript in the browser does not have the ability to operate files (based on security, it cannot directly operate local files), but JavaScript in Node has the ability to operate files. How does node read files? (You can ignore the code when installing node)
// 1. Use the require method to load the fs core module var fs = require('fs') // 2. Read file // The first parameter is the file path to be read // The second parameter is a callback function // Success // data data // error null // Failed // data undefined no data // error error object fs.readFile('read.txt', function (error, data) { // Here you can check if an error occurs by judging error if (error) { console.log('Failed to read the file') } else { console.log(data.toString()) } }) result File reading is an asynchronous operationWhen we read multiple files, we find that using readfile to read files does not necessarily print the results in order, so this is an asynchronous operation. How to read files sequentially. Using Promisesvar fs = require('fs') function pReadFile(filePath) { return new Promise(function (resolve, reject) { fs.readFile(filePath, 'utf8', function (err, data) { if (err) { reject(err) } else { resolve(data) } }) }) } pReadFile('./data/a.txt') .then(function (data) { console.log(data) return pReadFile('./data/b.txt') }) .then(function (data) { console.log(data) return pReadFile('./data/c.txt') }) .then(function (data) { console.log(data) }) Writing filesfs.writeFile('read.txt', 'Hello everyone, let me introduce myself to you. I am a file writer.', function (error) { if (error) { console.log('Write failed') } else { console.log('Written successfully') } }) The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Table of CSS Bugs Caused by hasLayout
>>: Implementation of CSS child element selection parent element
1. Global Object All modules can be called 1) glo...
First, let's simulate the data coming from th...
Background Here's what happened, Luzhu accide...
Preface Named slots are bound to elements using t...
1.MySQL UPDATE JOIN syntax In MySQL, you can use ...
In Linux, we usually use the mv command to rename...
Does color influence website visitors? A few year...
Table of contents What is front-end routing? How ...
Aggregating data from various sources allows the ...
Table of contents 1. Initialize the array 2. Arra...
Table of contents Scenario Code Implementation Su...
This article uses examples to illustrate the usag...
1. An error (1064) is reported when using mysqldu...
Detailed explanation of replace into example in m...
Let's take a look at the detailed method of b...