File API in JavaScript
Preview a selected text file in JavaScript with File.text and textContent, without uploading it. Check cancelled selection and file size before reading.
Save this as index.html and open it in a browser.
1<!doctype html>2<html lang="en">3<meta charset="utf-8">4<title>JavaScript practice</title>5<body>6<label>Choose a small text file <input id="file" type="file" accept=".txt,text/plain"></label><pre id="text"></pre>7<script>8const output = document.querySelector('#text');9document.querySelector('#file').onchange = async event => {10 const file = event.target.files[0];11 if (!file) { output.textContent = 'No file selected.'; return; }12 if (file.size > 100000) { output.textContent = 'Choose a file under 100 KB.'; return; }13 try { output.textContent = await file.text(); }14 catch { output.textContent = 'Could not read that file.'; }15};16</script>17</body>18</html>
Sample Output
Bring your notebook.
The selected file contains this sentence. A different file displays its own text.
In this one
- Read a user-selected file
- Handle a cancelled selection
- Limit file size before reading
- Display file contents as plain text
How it works
- 1
Create a small text file for a predictable test.
- 2
Choose it through the file input.
- 3
Check that a file exists and is under the example size limit.
- 4
Wait for file.text and catch reading errors.
- 5
The pre element displays exactly the chosen file text, including line breaks.
Edge cases
Selection cancelled
files[0] can be missing.
Return before trying to read file.size. Browser behavior on reopening and cancelling the picker can vary.
HTML inside text
A chosen file could contain markup.
Use textContent rather than innerHTML. The preview should show the characters, not turn them into active page content.
What's going on
The File API lets a browser page work with files that a person chooses. An API is a feature your code can call. A notes preview is a useful first project because it reads a small text file without uploading it anywhere.
Save the HTML example and create a .txt file containing Bring your notebook. The file input opens the browser's file picker. When the selection changes, files[0] is the first selected File object. file.text reads its contents and returns a promise, which gives the text after reading finishes.
The code checks for a cancelled selection and rejects large files before reading them all into memory. It displays the result with textContent, so text that looks like HTML is not executed. The accept attribute helps the picker show text files, but it is not a security check. This code expects ordinary text; images and other binary files will not produce a useful preview.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does accept guarantee?
Pick an answer to check it.
Question 2
What safely displays the file as text?
Pick an answer to check it.
Questions people ask
Does this upload my file?
No. It reads the selected file locally. Uploading would need separate network code.
Why use a pre element?
It keeps spaces and line breaks visible, which helps when previewing a text file.
Can JavaScript open any file on my computer?
Not with this code. It receives files that the person chooses through the input.