Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
638 B
1
Indexable
Never
const convertToBase64 = async (url) => {
  try {
    const response = await fetch(url);
    const blob = await response.blob();
    const reader = new FileReader();
    reader.readAsDataURL(blob);
    return new Promise((resolve) => {
      reader.onloadend = () => {
        resolve(reader.result);
      };
    });
  } catch (error) {
    console.error("Error converting to base64:", error);
    return null;
  }
};

// Convert all image URLs to base64
Promise.all(imageUrls.map((url) => convertToBase64(url)))
  .then((base64Images) => {
    console.log(base64Images);
    // base64Images is an array of base64-encoded image data
  });