Browser file handling means working with files the user chooses: inspect metadata, read text or bytes, preview images, then upload or process locally. The File API gives you File / FileList; FileReader (or file.text()) reads the contents.
Note No silent disk access
Scripts cannot open arbitrary paths. Files arrive only from a picker, drag-and-drop, or an API the user has approved.
DeprecatedreadAsBinaryString()
Prefer readAsArrayBuffer() or file.arrayBuffer() for binary data. Do not use readAsBinaryString in new code.
Limited support File System Access API
Permission-based open/save beyond a basic file input. Feature-detect before use; support is strongest in Chromium-based browsers.
Check size and type before reading large or wrong files.
Read with FileReader or promise helpers (file.text()), then show, parse, or upload.
Piece
Required?
Role
File input / drop
Yes
User grants access to a file
File / FileList
Yes
Metadata + blob of bytes
FileReader or file.text()
When reading
Decode contents asynchronously
revokeObjectURL
If blob URL
Free preview memory
Pseudocode
Pseudocode
on file input change:
file = files[0]
if no file: return
if file.size > MAX or type not allowed:
show error; clear input; return
// modern
text = await file.text()
show(text)
// or classic
reader = new FileReader()
reader.onload → show(reader.result)
reader.onerror → show error
reader.readAsText(file)
Reference
Cheat Sheet
Keep these File API facts handy.
Topic
Detail
Picker
<input type="file">
List
input.files → FileList
Metadata
name, size, type, lastModified
Text
readAsText / file.text()
Data URL
readAsDataURL → img.src
Blob URL
createObjectURL + revokeObjectURL
Binary
readAsArrayBuffer / file.arrayBuffer()
Server-side
Node.js fs — not the browser File API
Try it
Live Preview
Choose a file in this browser. The demo shows name, size, and MIME type — the same first step every file handler uses.
Nothing leaves your device — this preview only reads File metadata in the page.
Result
Pick a file to inspect metadata.
file.name → …
file.size → …
file.type → …
Code
JavaScript Examples
Short, copy-ready snippets. Pair each with a file input on the page, or open Try It Yourself for a live editor.
Example 1 — Inspect File Metadata
Log name, size, and MIME type as soon as the user picks a file — no reader needed.
Valid text contents appear here…
— or —
File is too large. Max size is 1 MB.
How It Works
Cap size early. Avoid loading huge files into memory.
Check type + extension. MIME can be empty; extension is a useful fallback.
Reset the input.fileInput.value = "" lets the user re-pick the same file.
Await text.file.text() returns a promise — wrap in try/catch for errors.
Edge Cases & Pitfalls
Check these before shipping file pickers.
security
accept is only a hint
Users can bypass the filter. Always validate size / type in JS and again on the server.
mime
Empty file.type
Some platforms leave MIME blank. Combine with extension checks when classifying files.
memory
Forgotten blob URLs
Call URL.revokeObjectURL when replacing previews so memory does not grow with every pick.
api
Browser ≠ Node fs
The File API runs in the page. Reading server disks uses Node fs (or fetch) — never expose raw server paths to clients.
Compatibility
Browser & Runtime Support
Browser File / FileReader and <input type="file"> are Baseline DOM APIs. In Node.js, read and write disk files with the fs / fs.promises module — not FileReader or a file input.
✓ Baseline · File API
File API & FileReader
Full support in Chrome, Firefox, Safari, Edge, and modern mobile browsers. Node.js / Bun / Deno: use fs (or equivalent) for server disk I/O. Global File/Blob exist in modern Node (20+), but picker + FileReader workflows stay browser-only.
98%Browser File API
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
Android WebViewSupported · Modern WebView
Full support
Internet ExplorerIE 10+ partial FileReader · Legacy only
Partial / obsolete
Node.jsUse fs / fs.promises · not FileReader
Different API
BunUse Bun.file / fs · not FileReader
Different API
DenoUse Deno.readFile · not FileReader
Different API
BrowsersExcellent
Bottom line: For web apps, the File API is production-safe. For Node.js servers, use fs.readFile / fs.promises.readFile (and related APIs) — do not expect browser FileReader or <input type="file"> in Node.
Analysis
Performance Notes
Approach
Cost
Best for
Metadata only
O(1)
Guards before reading
createObjectURL
Cheap handle
Large image previews
readAsDataURL
Base64 expand
Small images / need a data URL
Full readAsText
O(n) memory
Modest text / CSV / JSON
Reading loads the whole file into memory. Cap size on the client, prefer blob URLs for big media, and process huge uploads on the server (streaming / chunking) when possible.
Remember
Key Takeaways
Access: only user-selected files via picker or drop.
Read:FileReader or await file.text() / arrayBuffer().
Safety: validate in the browser for UX; re-validate on the server.
One line: pick a File, validate size/type, read with FileReader or file.text(), and revoke blob URLs when done.
Frequently Asked Questions
No. In the browser, scripts only get files the user picks through <input type="file">, drag-and-drop, or a permission-based picker such as the File System Access API. Arbitrary disk paths are not allowed.
A File object is the selected file (name, size, type, lastModified) and extends Blob. FileReader reads that File asynchronously and delivers the result through load, error, and abort events.
Both return UTF-8 text. Prefer await file.text() for short async handlers. Use FileReader when you need progress events, abort(), or older environments without Blob promise helpers.
Use URL.createObjectURL(file) for large image previews (faster, less memory). Use readAsDataURL when you need a data: URL string. Always call URL.revokeObjectURL when a blob URL is no longer needed.
Not to arbitrary paths. You can trigger a download with a Blob and an <a download> link, or use the File System Access API where supported after the user grants save permission. Server-side Node.js uses fs.
No. accept and file.type improve UX but are not a security boundary. Always re-validate size, type, and content on the server before trusting an upload.