前端上传图片预览

自古美人都是妖i 提交于 2019-12-03 03:49:49
转成blob 预览<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input id="inputFile" type="file" accept="image/*">
<img src="" id="previewImage" alt="图片预览">
<script>
    const $ = document.getElementById.bind(document);
    const $inputFile = $('inputFile');
    const $previewImage = $('previewImage');
    $inputFile.addEventListener('change', function(e) {
        const file = this.files[0];
        console.log(e)
        console.log(file)
        $previewImage.src = file ? URL.createObjectURL(file) : '';
    }, this);
</script>
</body>
</html>

 

图片转base64 (url 包含blob)

 static imageToBase64(imgUrl) {
        return new Promise((resolve, reject) => {
            var imgFile = new FileReader();
            imgFile.readAsDataURL(imgUrl);
            imgFile.onload = function () {
                var imgData = this.result; //base64数据
                resolve(imgData)

            }

        })


    }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!