java-script:read-file
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| java-script:read-file [2023/08/04 20:20] – odefta | java-script:read-file [2025/01/02 18:22] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| ===== Read a file in web browser ===== | ===== Read a file in web browser ===== | ||
| - | The following script reads the content of a file selected by the user and then displays those contents in a <pre> element. \\ When the "Read File" button is clicked, the readFile function is called, which reads the file selected in the file input field and writes its contents to the fileContents element. | + | The following script reads the content of a file selected by the user and then displays those contents in a **<pre>** element. \\ When the "Read File" button is clicked, the **readFile** function is called, which reads the file selected in the file input field and writes its contents to the **fileContents** element. |
| < | < | ||
| Line 25: | Line 25: | ||
| </ | </ | ||
| + | |||
| + | ==== Using callback function (on onload event) ==== | ||
| + | |||
| + | The **FileReader** object' | ||
| + | |||
| + | <code javascript read-file-web-callback.html> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | function readFile() { | ||
| + | const fileInput = document.getElementById(' | ||
| + | const file = fileInput.files[0]; | ||
| + | const reader = new FileReader(); | ||
| + | |||
| + | reader.onload = function(e) { | ||
| + | const contents = e.target.result; | ||
| + | document.getElementById(' | ||
| + | }; | ||
| + | |||
| + | reader.readAsText(file); | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | < | ||
| + | <input type=" | ||
| + | <button onclick=" | ||
| + | <pre id=" | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ==== Using Promise ==== | ||
| + | |||
| + | If you prefer using **Promise** instead of the **onload** event, you can create a wrapper function around the FileReader API that returns a promise. \\ | ||
| + | A new function **readAsText** wraps the FileReader API and returns a promise. This promise resolves with the file's contents when the onload event is fired, and rejects if there' | ||
| + | |||
| + | <code javascript read-file-web-promise.html> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | function readFile() { | ||
| + | const fileInput = document.getElementById(' | ||
| + | const file = fileInput.files[0]; | ||
| + | |||
| + | readAsText(file).then(contents => { | ||
| + | document.getElementById(' | ||
| + | }); | ||
| + | } | ||
| + | |||
| + | function readAsText(file) { | ||
| + | return new Promise((resolve, | ||
| + | const reader = new FileReader(); | ||
| + | |||
| + | reader.onload = function(e) { | ||
| + | resolve(e.target.result); | ||
| + | }; | ||
| + | | ||
| + | reader.onerror = function(e) { | ||
| + | reject(e); | ||
| + | }; | ||
| + | |||
| + | reader.readAsText(file); | ||
| + | }); | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | < | ||
| + | <input type=" | ||
| + | <button onclick=" | ||
| + | <pre id=" | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ===== Read a file in Node.js ===== | ||
| + | |||
| + | First you should [[java-script: | ||
| + | |||
| + | <code javascript fileReader.js> | ||
| + | import fs from ' | ||
| + | |||
| + | export async function readFile(filePath) { | ||
| + | try { | ||
| + | const data = await fs.readFile(filePath, | ||
| + | console.log(' | ||
| + | } catch (err) { | ||
| + | console.error(' | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code javascript index.js> | ||
| + | import { readFile } from ' | ||
| + | |||
| + | const fileName = process.argv[2]; | ||
| + | if (!fileName) { | ||
| + | console.log(' | ||
| + | process.exit(1); | ||
| + | } | ||
| + | |||
| + | readFile(fileName); | ||
| + | </ | ||
| + | |||
| + | To run it: | ||
| + | < | ||
| + | node ./index.js C:/test.txt | ||
| + | </ | ||
java-script/read-file.1691180440.txt.gz · Last modified: (external edit)
