Skip to content

Instantly share code, notes, and snippets.

@n1ru4l
Created August 7, 2017 10:47
Show Gist options
  • Select an option

  • Save n1ru4l/dc99062577b746e0783410b1298ab897 to your computer and use it in GitHub Desktop.

Select an option

Save n1ru4l/dc99062577b746e0783410b1298ab897 to your computer and use it in GitHub Desktop.
Fetch blob and convert to base64
export const fetchAsBlob = url => fetch(url)
.then(response => response.blob());
export const convertBlobToBase64 = blob => new Promise((resolve, reject) => {
const reader = new FileReader;
reader.onerror = reject;
reader.onload = () => {
resolve(reader.result);
};
reader.readAsDataURL(blob);
});
//fetchAsBlob(`https://fonts.gstatic.com/s/roboto/v16/d-6IYplOFocCacKzxwXSOJBw1xU1rKptJj_0jans920.woff2`)
// .then(convertBlobToBase64)
// .then(console.log)
@surajsharma
Copy link
Copy Markdown

this has been invaluable, thanks :)

@lapc18
Copy link
Copy Markdown

lapc18 commented Aug 5, 2021

thank you so much, this snippet will save the night!

@vishu3278
Copy link
Copy Markdown

Thanks for the solution.

@pavelglebov
Copy link
Copy Markdown

Awesome! Thank you!

@criticalmiind
Copy link
Copy Markdown

criticalmiind commented Jul 10, 2022

export const fetchAsBlob = url => fetch(url).then(response => response.blob()); 
not working in React Native android and Nodejs, working for iOS!
any alternative solution

@duty-bound
Copy link
Copy Markdown

Worked for me on an Android emulator, many thanks!

@kbrattli
Copy link
Copy Markdown

kbrattli commented Sep 9, 2024

Thanks, this was perfect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment