html——上传图片并转为base64编码字符串


<html>
<head>
    <meta charset="UTF-8">
    <title>图片转码测试title>
head>
<body>
<input accept="image/*" name="upimage" id="upload_file" type="file">
<textarea id="base64_output" name="Word" style=" width:820px">textarea>
<script type="text/javascript">
    
    var str;//全局变量,用于存储base64字符串
    document.getElementById("upload_file").onchange = function () {
        var str=gen_base64();

    };
    //图片转为base64编码字符串
    function gen_base64() {
        var file = document.getElementById('upload_file').files[0];
        var r = new FileReader();  //本地预览

        r.readAsDataURL(file);    //Base64
        r.onload = function(){
            document.getElementById('base64_output').value = r.result;

            str=r.result;
            console.info(str);
            return r.result;
        }


    }
script>
body>
html>

参考:
JS上传图片转化成Base64编码demo

你可能感兴趣的:(Html+Js,转载,javascript,js,base64)