10 examples of 'javascript read local file into string' in JavaScript

Every line of 'javascript read local file into string' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
8function readFile(file) {
9 return ''+new String(org.apache.tools.ant.util.FileUtils.readFully(new java.io.FileReader(file)));
10}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

4export function readFromFile (fileName, onSuccess, onFail) {
5 const pathToFile = cordova.file.dataDirectory + fileName
6 window.resolveLocalFileSystemURL(
7 pathToFile,
8 function (fileEntry) {
9 fileEntry.file(function (file) {
10 var reader = new FileReader()
11
12 reader.onloadend = function (e) {
13 onSuccess(this.result)
14 }
15
16 reader.readAsText(file)
17 }, onFail
18 )
19 }, onFail
20 )
21}
50function readFile(file: File, callback: (contents: string) => void): void {
51 if (!global.FileReader || (file.type && !(file.type in TEXT_TYPES))) {
52 callback('');
53 return;
54 }
55
56 if (file.type === '') {
57 let contents = '';
58 // Special-case text clippings, which have an empty type but include
59 // `.textClipping` in the file name. `readAsText` results in an empty
60 // string for text clippings, so we force the file name to serve
61 // as the text value for the file.
62 if (TEXT_CLIPPING_REGEX.test(file.name)) {
63 contents = file.name.replace(TEXT_CLIPPING_REGEX, '');
64 }
65 callback(contents);
66 return;
67 }
68
69 const reader = new FileReader();
70 reader.onload = function() {
71 const result = reader.result;
72 invariant(
73 typeof result === 'string',
74 'We should be calling "FileReader.readAsText" which returns a string',
75 );
76 callback(result);
77 };
78 reader.onerror = function() {
79 callback('');
80 };
81 reader.readAsText(file);
82}
26function read (file) {
27 return fs.readFileSync(file, {encoding: 'utf-8'})
28}
62function _readFile(localPath) {
63 // WARN: This only works on NodeJS
64 return new Promise((resolve, reject) => {
65 fs.readFile(localPath, 'utf8', (err, data) => {
66 if (err) {
67 reject(err)
68 } else {
69 resolve(data)
70 }
71 })
72 })
73}
11function getAsText(fileToRead) {
12 var reader = new FileReader();
13 // Handle errors load
14 reader.onload = loadHandler;
15 reader.onerror = errorHandler;
16 // Read file into memory as UTF-8
17 reader.readAsText(fileToRead);
18}
9export function getStringFromFile(location: string) {
10 const privateKeyFile = path.resolve(location);
11 const privateKey = fs.readFileSync(privateKeyFile, 'utf8');
12 return privateKey;
13}
21function readFileAsDataURL(file, callback) {
22 var reader;
23
24 // Use FileReader if it's available
25 if ("FileReader" in window) {
26 reader = new FileReader();
27 reader.readAsDataURL(file);
28 reader.onload = function() {
29 callback(reader.result);
30 };
31 } else {
32 return callback(file.getAsDataURL());
33 }
34}
89function read(filename){
90 return fs.readFileSync(filename, { encoding: 'utf8' });
91}
21function evalFileInRemote(file: string) {
22 if (!path.isAbsolute(file)) {
23 file = path.join(UserDataPath, file);
24 }
25 fs.readFile(file, 'utf8', (err, source) => {
26 if (err) {
27 log.error('Error while loading JavaScript source from file ' + file, err);
28 return;
29 }
30 const elem = Store.getState().webview.element;
31 if (!elem) {
32 return;
33 }
34 elem.executeJavaScript(source);
35 });
36}

Related snippets