krot

image to base64

Oct 27th, 2021
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //https://stackoverflow.com/questions/6150289/how-can-i-convert-an-image-into-base64-string-using-javascript
  2. const toDataURL = url => fetch(url)
  3.   .then(response => response.blob())
  4.   .then(blob => new Promise((resolve, reject) => {
  5.     const reader = new FileReader()
  6.     reader.onloadend = () => resolve(reader.result)
  7.     reader.onerror = reject
  8.     reader.readAsDataURL(blob)
  9.   }))
  10.  
  11.  
  12. toDataURL('https://www.gravatar.com/avatar/d50c83cc0c6523b4d3f6085295c953e0')
  13.   .then(dataUrl => {
  14.     console.log('RESULT:', dataUrl)
  15.   })
Add Comment
Please, Sign In to add comment