下载远程图片转 Base64

<html>
<head>
head>
<body>
<img id="img" src=""/>
<button id="btn">下载button>
<script>

window.onload = function () {
let btn = this.document.getElementById("btn");
btn.onclick = function () {
startDownload();
};
};

function startDownload() {
let imageURL = "http://192.168.0.100:8081/oss/posters/12345678.png";
//imageURL = "https://cdn.glitch.com/4c9ebeb9-8b9a-4adc-ad0a-238d9ae00bb5%2Fmdn_logo-only_color.svg?1535749917189";
downloadedImg = new Image;
downloadedImg.crossOrigin = "Anonymous";
downloadedImg.addEventListener("load", imageReceived, false);
downloadedImg.src = imageURL;
}

function imageReceived() {
let canvas = document.createElement("canvas");
let context = canvas.getContext("2d");

canvas.width = downloadedImg.width;
canvas.height = downloadedImg.height;

context.drawImage(downloadedImg, 0, 0);

try {
let data = canvas.toDataURL("image/png");
let img = document.getElementById("img");
img.src = data;
}
catch (err) {
console.log("Error: " + err);
}
}
script>
body>
html>

Tags: canvas base64 image