Created
August 1, 2017 12:29
-
-
Save mgenov/b35b50525e52cab11326a3f5bcf17611 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const b64toBlob = (b64Data, contentType='', sliceSize=512) => { | |
const byteCharacters = atob(b64Data); | |
const byteArrays = []; | |
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) { | |
const slice = byteCharacters.slice(offset, offset + sliceSize); | |
const byteNumbers = new Array(slice.length); | |
for (let i = 0; i < slice.length; i++) { | |
byteNumbers[i] = slice.charCodeAt(i); | |
} | |
const byteArray = new Uint8Array(byteNumbers); | |
byteArrays.push(byteArray); | |
} | |
const blob = new Blob(byteArrays, {type: contentType}); | |
return blob; | |
} | |
const contentType = 'image/png'; | |
const b64Data = 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='; | |
const blob = b64toBlob(b64Data, contentType); | |
const blobUrl = URL.createObjectURL(blob); | |
const img = document.createElement('img'); | |
img.src = blobUrl; | |
document.body.appendChild(img); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment