Javascript snippets

Jul 22, 2022 · TidbitJavaScript

1. Get base64 from URL

async function getBase64FromUrl(URL) {
  const data = await fetch(URL);
  const blob = await data.blob();
  return new Promise((resolve) => {
    const reader = new FileReader();
    reader.readAsDataURL(blob);
    reader.onloadend = () => {
      const base64data = reader.result;
      resolve(base64data);
    };
  });
};
© Nurul Uhkrowi 2024