10 examples of 'javascript readfile' in JavaScript

Every line of 'javascript readfile' 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
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}
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

8function readFile(file) {
9 return ''+new String(org.apache.tools.ant.util.FileUtils.readFully(new java.io.FileReader(file)));
10}
11function readFile(dataFile){
12 return fs.readFileSync(dataFile);
13}
14function readFile(filename) {
15 return fs.readFileSync(filename, { encoding: 'UTF-8' });
16}
2static readFile(filePath, callback) {
3 const file = window.location.origin + window.location.pathname + filePath;
4 const rawFile = new XMLHttpRequest();
5 rawFile.open("GET", file, false);
6 rawFile.onreadystatechange = () => {
7 callback(rawFile.responseText);
8 }
9 rawFile.send(null);
10}
12function readFile(filename, encoding, callback) {
13 if (options.file === '-') {
14 // read from stdin
15 const chunks = [];
16
17 process.stdin.on('data', (chunk) => {
18 chunks.push(chunk);
19 });
20
21 process.stdin.on('end', () => callback(null, Buffer.concat(chunks).toString(encoding)));
22 } else {
23 fs.readFile(filename, encoding, callback);
24 }
25}
102readFile(fileName) {
103 return this._host.readFile(fileName);
104}
157function readFile() {
158 function onFileReadAsDataURL(evt) {
159 var img = document.getElementById('camera_image');
160 img.style.visibility = "visible";
161 img.style.display = "block";
162 img.src = evt.target.result;
163 log("FileReader.readAsDataURL success");
164 };
165
166 function onFileReceived(file) {
167 log('Got file: ' + JSON.stringify(file));
168 fileObj = file;
169
170 var reader = new FileReader();
171 reader.onload = function() {
172 log('FileReader.readAsDataURL() - length = ' + reader.result.length);
173 };
174 reader.onerror = logCallback('FileReader.readAsDataURL', false);
175 reader.readAsDataURL(file);
176 };
177 // Test out onFileReceived when the file object was set via a native <input> elements.
178 if (fileObj) {
179 onFileReceived(fileObj);
180 } else {
181 fileEntry.file(onFileReceived, logCallback('FileEntry.file', false));
182 }
183}
123function readFile() {
124 function onFileReadAsDataURL(evt) {
125 var img = document.getElementById('camera_image');
126 img.style.visibility = "visible";
127 img.style.display = "block";
128 img.src = evt.target.result;
129 log("FileReader.readAsDataURL success");
130 };
131
132 function onFileReceived(file) {
133 log('Got file: ' + JSON.stringify(file));
134 fileObj = file;
135
136 var reader = new FileReader();
137 reader.onload = function() {
138 log('FileReader.readAsDataURL() - length = ' + reader.result.length);
139 };
140 reader.onerror = logCallback('FileReader.readAsDataURL', false);
141 reader.readAsDataURL(file);
142 };
143 // Test out onFileReceived when the file object was set via a native <input> elements.
144 if (fileObj) {
145 onFileReceived(fileObj);
146 } else {
147 fileEntry.file(onFileReceived, logCallback('FileEntry.file', false));
148 }
149}
51export function readFile(filePath: string): string {
52 return fs.readFileSync(filePath, 'utf8');
53}

Related snippets