Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
The File API lets you read files that users select through file inputs. You can read text files, images, or any file type and display or process their content!
Upload and preview files!
<div style="max-width: 600px; margin: 0 auto; font-family: sans-serif;">
<h2 style="color: #2563eb; margin-bottom: 20px;">📁 File Reader Demo</h2>
<!-- File Input -->
<div style="background: #dbeafe; padding: 20px; border-radius: 8px; margin-bottom: 20px; border: 2px solid #93c5fd;">
<h3 style="color: #1e40af; margin-top: 0;">Choose a Text File:</h3>
<input type="file" id="fileInput" accept=".txt,.json,.html,.css,.js" style="width: 100%; padding: 12px; border: 2px solid #60a5fa; border-radius: 6px; background: white;">
<p style="font-size: 12px; color: #1e40af; margin-top: 8px; margin-bottom: 0;">Accepts: .txt, .json, .html, .css, .js files</p>
</div>
<!-- File Info -->
<div id="fileInfo" style="background: #f0f9ff; padding: 15px; border-radius: 6px; margin-bottom: 15px; display: none; border-left: 4px solid #3b82f6;">
<h4 style="margin: 0 0 10px 0; color: #1e40af;">📄 File Information:</h4>
<div id="fileDetails" style="font-size: 14px; color: #1e3a8a;"></div>
</div>
<!-- File Content -->
<div id="contentArea" style="display: none;">
<h3 style="color: #2563eb;">📝 File Content:</h3>
<div style="background: #f8fafc; padding: 15px; border-radius: 8px; border: 2px solid #bfdbfe;">
<pre id="fileContent" style="margin: 0; white-space: pre-wrap; font-family: monospace; font-size: 13px; color: #1e293b; max-height: 400px; overflow-y: auto;"></pre>
</div>
</div>
</div>Loading preview...
// Read text file content
document.getElementById('fileInput').addEventListener('change', (event) => {
const file = event.target.files[0];
if (!file) return;
// Create FileReader
const reader = new FileReader();
// Set up onload handler
reader.onload = (e) => {
const text = e.target.result;
console.log('File content:', text);
document.getElementById('output').textContent = text;
};
// Read file as text
reader.readAsText(file);
});
// HTML:
// <input type="file" id="fileInput" accept=".txt">
// <pre id="output"></pre>
// 🎯 FileReader methods:
// - readAsText() - Read as text
// - readAsDataURL() - Read as base64 (for images)
// - readAsArrayBuffer() - Read as binary data// Preview image before upload
document.getElementById('imageInput').addEventListener('change', (event) => {
const file = event.target.files[0];
if (!file || !file.type.startsWith('image/')) {
alert('Please select an image file');
return;
}
const reader = new FileReader();
reader.onload = (e) => {
// e.target.result contains base64 image data
const img = document.getElementById('preview');
img.src = e.target.result;
img.style.display = 'block';
};
// Read as data URL (base64)
reader.readAsDataURL(file);
});
// HTML:
// <input type="file" id="imageInput" accept="image/*">
// <img id="preview" style="max-width: 300px; display: none;">
// 🎯 Common use: Profile picture upload with previewShow image before uploading
Read and parse CSV/Excel files
Open and edit text files
Import JSON configuration files
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = (e) => console.log(e.target.result);
reader.readAsText(file);reader.readAsDataURL(file); // Returns base64🔒 Security: Can only read user-selected files
Main API for reading files
Async with events
readAsText()
readAsDataURL()
Only user-selected files
Cannot access randomly
Image previews
CSV/JSON imports