1、本地图片 File 对象转换为 Data URL
function loadFile(event) {
const reader = new FileReader();
reader.onload = function () {
const image = document.querySelector("#image");
image.src = reader.result;
};
reader.readAsDataURL(event.target.files[0]);
}
读取图片
2、Base64
btoa Binary-to-ASCII atob ASCII-to-Binary
console.log(btoa('BC')); // QkM=
console.log(atob('QkM=')); // BC
3、fetch API 从网络上获取图片
const image = document.querySelector("#image");
fetch("https://avatars3.githubusercontent.com/u/4220799")
.then((response) => response.blob())
.then((blob) => {
const objectUrl = URL.createObjectURL(blob);
image.src = objectUrl;
});
读取图片
Blob(Binary Large Object)表示二进制类型的大对象
const blob = new Blob(['hello'])
参考 前端二进制