Skip to content

Instantly share code, notes, and snippets.

@anton-karlovskiy
Forked from n1ru4l/index.js
Last active June 30, 2020 21:03
Show Gist options
  • Save anton-karlovskiy/8e35f70976b6cdd127b7046ae9c2c9aa to your computer and use it in GitHub Desktop.
Save anton-karlovskiy/8e35f70976b6cdd127b7046ae9c2c9aa to your computer and use it in GitHub Desktop.
Fetch blob and convert to base64
const fetchAsBlob = url => fetch(url).then(response => response.blob());
const convertBlobToBase64 = blob => new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onerror = reject;
reader.onload = () => {
resolve(reader.result);
};
reader.readAsDataURL(blob);
});
// e.g.
fetchAsBlob('https://fonts.gstatic.com/s/roboto/v16/d-6IYplOFocCacKzxwXSOJBw1xU1rKptJj_0jans920.woff2')
.then(convertBlobToBase64)
.then(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment