zoukankan      html  css  js  c++  java
  • 利用FileReader对象回显图片

    html:

    <div class="btn-box">
    <button class="btnImg">上传图片</button>
    <input id="file" type="file" name="pictureFile" class="file-ipt">
    <img class="img" id="showimg" width="200px" height="200px" src="" style="display: none"/>
    </div>

    js:

    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js">
    </script>
    <script type="text/javascript">
    var showimg = document.getElementById('showimg');
    var fileBtn = document.getElementById('file');
    $('.btnImg').on('click',function(){
    $('#file').click() //点击按钮触发input
    })
    // 获取上传文件信息
    fileBtn.onchange = function () {
    var file = this.files[0];
    if(window.FileReader) {
    var fr = new FileReader();
    fr.readAsDataURL(file);
    fr.onload = function(e) {
    console.log(e.target); // e.target返回FileReader对象,里面包含:事件,状态,属性,结果等
    console.log(this); // 同e.target返回结果一样,两者等价
    console.log(e.target.result); // 读取的结果,dataURL格式的
    showimg.style.display="inline"
    showimg.src = this.result; // 图片可显示出来
    };
    } else {
    alert('暂不支持FileReader');
    };
    };
    </script>

    css:
    .img {
    200px;
    height: 200px;
    border: 1px solid rgb(143, 59, 59);
    border-radius: 5px;
    }
    .file-ipt {
    height: 0;
    }
     
    .btnImg {
    100px;
    height: 30px;
    background-color: skyblue;
    color: white;
    margin-right: 80px;
    border: none;
    border-radius: 10px;
    margin-bottom: 10px;
    }


    >   From 子枫i

    如果该文章对你有帮助的话,不妨点个赞
  • 相关阅读:
    给脚本绑定LUA解释器
    Flash Socket连接受限解决方法
    使用CMake构建编译环境
    如何使用OpenCL编写程序
    使用zzip和minizip解压缩文件
    使用CURL库下载文件
    使用GDI+保存位图文件为PNG文件
    linux学习
    Tomcat6连接池配置
    ArrayList与LinkedList性能差别
  • 原文地址:https://www.cnblogs.com/guomouren/p/14958184.html
Copyright © 2011-2022 走看看