Real-world use cases for Base64 in web apps — data URIs, API authentication, file uploads, and inline assets.
Embed small images directly in HTML or CSS without extra HTTP requests:
<img src="data:image/png;base64,iVBORw0KGgo..." alt="icon" />
Best for images under 2KB. Larger images inflate HTML more than they save on requests.
HTTP Basic Auth sends credentials as Base64(username:password) in the Authorization header:
const credentials = btoa("admin:s3cr3t");
fetch("/api/data", {
headers: { Authorization: `Basic ${credentials}` }
});
Always use HTTPS — Base64 is not encryption.
Encode files to send them as JSON instead of multipart form data:
function fileToBase64(file) {
return new Promise((resolve) => {
const reader = new FileReader();
reader.onload = (e) => resolve(e.target.result.split(',')[1]);
reader.readAsDataURL(file);
});
}
const base64 = await fileToBase64(file);
await fetch('/api/upload', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ filename: file.name, data: base64 })
});