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
MySQL escape Escape means the original semantics ...
MySQL 8 brings a brand new experience, such as su...
REPLACE Syntax REPLACE(String,from_str,to_str) Th...
Tomcat7.0 sets virtual directory (1) Currently, o...
Table of contents Preface How to implement Vuex f...
Problem description: After executing docker run -...
Detailed explanation of Linux LVM logical volume ...
Why use prettier? In large companies, front-end d...
Table of contents 1. Ubuntu source change 2. Inst...
Overview I have been using Docker for more than a...
background go-fastdfs is a distributed file syste...
Recently, when I was working on a conference heal...
There are already many articles about slot-scope ...
Using win docker-desktop, I want to connect to co...
Local Windows remote desktop connects to Alibaba ...