美文网首页
图片url转base64编码,作为参数传给后台,亲测有效

图片url转base64编码,作为参数传给后台,亲测有效

作者: 爱吃香菜的憨憨 | 来源:发表于2019-06-25 21:09 被阅读0次

    通过选择上传本地图片,将图片转成base64格式传递给后台

    html

    <div class="upload-file">
        <span>选择文件</span>
        <img src="" id="fileUrl" alt="" style="display: none;">
        <input type="file" id="file" data-src="" class="img-input" multiple="multiple" @change="getFile">
    </div>
    

    js

    getFile() {
           const filePath = document.getElementById('file').files[0];
           const reader = new FileReader();
           reader.readAsDataURL(filePath);
           reader.onload = () => {
                 const img = document.getElementById('fileUrl');
                 img.src = reader.result;
            };
    }
    // 将url存入src才可通过获取dom获取到路径
    const imgBase64 = $('#fileUrl')[0].src;
    imgBase64作为参数传递给后台
    

    相关文章

      网友评论

          本文标题:图片url转base64编码,作为参数传给后台,亲测有效

          本文链接:https://www.haomeiwen.com/subject/xjqfcctx.html