FileReader reads local files or blobsThe FileReader object provides asynchronous reading of the contents of files stored on the user's computer. Use the File or Blob object to specify the file or data to be read. The FileReader interface provides methods for reading files and an event model that includes the reading results. 1. Use of FileReaderNote: If you need to be compatible with older browsers, you need to check whether the FileReader object exists. if (window.FileReader) { let reader = new FileReader(); } else { console.log('Your browser does not support reading files'); } 2. FileReader Methods
3. FileReader properties
4. FileReader events
Notice: 1. Due to security reasons, FileReader reads files passed in by input or files returned by ajax reading server, and cannot read files in the specified path. 2. FileReader can be used in webworker. <!DOCTYPE html> <html class="no-js"> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <title></title> <meta name="description" content="" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <link rel="stylesheet" href="" /> </head> <body> <input type="file" id="myFile" /> <script type="text/javascript"> if (window.FileReader) { var reader = new FileReader(); } else { console.log('Your browser does not support reading files'); } var myFile = document.querySelector('#myFile'); myFile.onchange = function () { var file = myFile.files[0]; reader.readAsDataURL(file); reader.onload = function () { var data = reader.result; //file content in base64 format }; reader.onerror = function(){ console.log('Read failed'); console.log(reader.error); } }; </script> </body> </html> Problems with using FileReader to read local disk filesExecute a js file (place the js file under the src of the project) (1) java.net.URL url = TestScriptEngine.class.getClassLoader().getResource("a.js"); (2)//System.out.println(url.getPath().substring(1).replace("%20", " ")); (3)FileReader fileReader = new FileReader(url.getPath()); Runtime
If I change url.getPath() to "D:/Eclipse WorkSpace/(java300)ScriptManager/bin/a.js" my local file directory, I can read the file successfully The value of url.getPath() is: D:\Eclipse%20WorkSpace\(java300)ScriptManager\bin\a.js But the loading file cannot be found The problem is "%20". There is a space between Eclipse WorkSpace, and the system automatically replaces it with %20, causing an error during operation. Do some processing on url.getPath(), replace %20 with " " space by url.getPath().substring(1).replace("%20", " "); the problem is solved 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:
|
<<: CSS screen size adaptive implementation example
>>: How to use crontab to backup MySQL database regularly in Linux system
Table of contents 1. Form events 2. Mouse events ...
1. Use curl command to access by default: # curl ...
When creating a time field DEFAULT CURRENT_TIMEST...
1. Unzip mysql-8.0.21-winx64 2. Configure environ...
Table of contents Pull a centos image Generate ng...
Docker Swarm is a container cluster management se...
This article example shares the implementation me...
MySQL needs to be upgraded to version 5.5.3 or ab...
I recently wanted to convert a website to https a...
Sometimes you need to use links, but you don't...
Table of contents 1. Official Documentation 2. Cr...
1. Execute the select statement first to generate...
When Mysql occupies too much CPU, where should we...
This article records the graphic tutorial of MySQ...
Preface In the early stages of some projects, dev...