您当前的位置: 首页 >  前端

JS:前端二进制

彭世瑜 发布时间:2020-11-05 11:06:38 ,浏览量:2

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'])

参考 前端二进制

关注
打赏
1688896170
查看更多评论

彭世瑜

暂无认证

  • 2浏览

    0关注

    2771博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0462s