Skip to content

Instantly share code, notes, and snippets.

@seguri
Last active February 4, 2026 20:05
Show Gist options
  • Select an option

  • Save seguri/9760ef037bb266f0bd1ce6df457610b2 to your computer and use it in GitHub Desktop.

Select an option

Save seguri/9760ef037bb266f0bd1ce6df457610b2 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Base64 Decoder & Downloader</title>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-family: system-ui, -apple-system, sans-serif;
background: #f5f5f5;
padding: 20px;
}
.container {
width: 100%;
max-width: 800px;
display: flex;
flex-direction: column;
gap: 16px;
}
textarea {
width: 100%;
height: 400px;
padding: 16px;
font-family: monospace;
font-size: 14px;
border: 2px solid #ccc;
border-radius: 8px;
resize: vertical;
}
textarea:focus {
outline: none;
border-color: #0066cc;
}
.controls {
display: flex;
gap: 12px;
align-items: center;
}
input[type="text"] {
flex: 1;
padding: 12px 16px;
font-size: 16px;
border: 2px solid #ccc;
border-radius: 8px;
}
input[type="text"]:focus {
outline: none;
border-color: #0066cc;
}
button {
padding: 12px 32px;
font-size: 16px;
font-weight: 600;
color: white;
background: #0066cc;
border: none;
border-radius: 8px;
cursor: pointer;
transition: background 0.2s;
}
button:hover {
background: #0052a3;
}
button:active {
background: #004080;
}
.error {
color: #cc0000;
font-size: 14px;
min-height: 20px;
}
</style>
</head>
<body>
<div class="container">
<textarea id="base64Input" placeholder="Paste base64 encoded data here..."></textarea>
<div class="controls">
<input type="text" id="filename" placeholder="Filename (e.g., file.pdf)" value="download.bin">
<button id="downloadBtn">Download</button>
</div>
<div class="error" id="error"></div>
</div>
<script>
const textarea = document.getElementById('base64Input');
const filenameInput = document.getElementById('filename');
const downloadBtn = document.getElementById('downloadBtn');
const errorDiv = document.getElementById('error');
const atoblob = (base64) => {
const binaryString = atob(base64);
const chars = Array.from(binaryString, c => c.charCodeAt(0));
const bytes = new Uint8Array(chars);
return new Blob([bytes], { type: 'application/octet-stream' });
}
downloadBtn.addEventListener('click', () => {
errorDiv.textContent = '';
let base64 = textarea.value.trim();
if (!base64) {
errorDiv.textContent = 'Please paste base64 data first.';
return;
}
// Remove data URL prefix if present (e.g., "data:application/pdf;base64,")
const dataUrlMatch = base64.match(/^data:[^;]+;base64,(.+)$/);
if (dataUrlMatch) {
base64 = dataUrlMatch[1];
}
// Remove whitespace/newlines that might be in the base64 string
base64 = base64.replace(/\s/g, '');
try {
const blob = atoblob(base64);
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filenameInput.value.trim() || 'download.bin';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
} catch (e) {
errorDiv.textContent = 'Invalid base64 data: ' + e.message;
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment