Skip to content

Instantly share code, notes, and snippets.

@dimitrovs
Created December 27, 2020 03:14
Show Gist options
  • Save dimitrovs/d29ace067e56306d89d9f6de4ce75361 to your computer and use it in GitHub Desktop.
Save dimitrovs/d29ace067e56306d89d9f6de4ce75361 to your computer and use it in GitHub Desktop.
Blob<>JSON
// 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