上传图片,并转换为base64,结果显图和编码

chenyajun  2022-10-16 14:04:20  阅读 1191 次 评论 0 条
<html>    <head>    <style>    
    </style>    </head>    <body>    <h3 align="center">上传图片,并转换为base64,这样体积会多出三分之一</h3>    <hr>    <input id="inp" type="file">    <p id="b64"></p>    <img id="img">    <script type="text/javascript">    
    function EL(id) {    
        return document.getElementById(id);    
    } // Get el by ID helper function    
    function readFile() {    
        if (this.files && this.files[0]) {    
            var FR = new FileReader();    
            FR.onload = function (e) {    
                EL("img").src = e.target.result;    
                EL("b64").innerHTML = e.target.result;    
            };    
            FR.readAsDataURL(this.files[0]);    
        }    
    }    
    EL("inp").addEventListener("change", readFile, false);    </script>    </body>    </html>


本文地址:https://www.chenyajun.net/index.php/post/106.html
版权声明:本文为原创文章,版权归 chenyajun 所有,欢迎分享本文,转载请保留出处!

评论已关闭!