{ if (!response.ok) { return response.json().then(errData => { const errorMsg = errData.error || errData.message || "Invalid image data"; throw new Error(errorMsg); }); } return response.json(); }) .then(data => { if (data.success) { this.decodedImageSrc = data.data; this.decodedMime = data.mime; } }) .catch(error => { console.error('Validation failed:', error); // Global AJAX interceptor will catch 422/500 errors and trigger the home redirect. // But we add a local fallback just in case: if (error.message.toLowerCase().includes("corrupt") || error.message.toLowerCase().includes("invalid base64")) { alert("Invalid or corrupt image data detected. Returning to home page safely."); window.location.href = "/"; } }); }, downloadDecoded() { if (!this.decodedImageSrc) return; const ext = this.decodedMime.split('/')[1] || 'png'; const link = document.createElement('a'); link.href = this.decodedImageSrc; link.download = 'decoded_image.' + ext; document.body.appendChild(link); link.click(); document.body.removeChild(link); }, clearDecoder() { this.base64DecodedInput = ''; this.decodedImageSrc = ''; } }">
Back to Tools

Image to Base64 Converter

19 runs
Drag and drop your image file here or click to browse from device
PNG, JPG, SVG, WEBP