在JavaScript中,您可以使用Fetch API和Blob

11223| 阅读:430 发表时间:2024-01-02 15:53:30 科技智能

async function fetchImageAndConvertToBase64(imageUrl) {
// Fetch the image
const response = await fetch(imageUrl);

// Check if the fetch was successful
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

// Create a blob object from the response
const blob = await response.blob();

// Convert the blob object to a Base64 encoded string
const base64String = await new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(blob);
});

return base64String;
}


*文章为投稿者独立观点,不代表本站站长的立场
本文由相遇青春-Yn发表并编辑,转载此文章须经作者同意,并请附上出处及本页链接。如有侵权,请联系本站删除。

热门推荐