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
one. Remote deployment using tomcat 1.1 Problems ...
The json data must be returned in html format That...
Prepare Environmental information for this articl...
Create a project directory mkdir php Create the f...
Preface The following are the ways to implement L...
Effect screenshots: Implementation code: Copy code...
MySQL's index types include normal index, uni...
There is a business that queries the 5 most recen...
1. Log in to VPN using IE browser 2. Remote login...
A brief analysis of rem First of all, rem is a CS...
Table of contents 1. Cartesian product phenomenon...
Ubuntu install jdk: [link] Install Eclipse on Ubu...
This is the effect of the Element UI loading comp...
Preface I have been working on some front-end pro...
Table of contents 1. Observable 2. Higher-order f...