Created
December 27, 2020 03:14
-
-
Save dimitrovs/d29ace067e56306d89d9f6de4ce75361 to your computer and use it in GitHub Desktop.
Blob<>JSON
This file contains hidden or 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
// credits: https://stackoverflow.com/questions/27232604/json-stringify-or-how-to-serialize-binary-data-as-base64-encoded-json | |
// Blob to JSON | |
const blobToBase64 = (blob) => { | |
return new Promise((resolve) => { | |
const reader = new FileReader(); | |
reader.readAsDataURL(blob); | |
reader.onloadend = function () { | |
resolve(reader.result); | |
}; | |
}); | |
}; | |
(async () => { | |
const b64 = await blobToBase64(blob); | |
const jsonString = JSON.stringify({blob: b64}); | |
console.log(jsonString); | |
})(); | |
// JSON to Blob | |
const parsed = JSON.parse(jsonString); | |
const blob = await fetch(parsed.blob).then(res => res.blob()); | |
console.log(blob); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment