• 【前端预览】实现多张上传图片预览查看


    遇到平台页面开发需要提交上传多张证书图片,之前也做过预览,但貌似都是单张或者存到隐藏域交给后台处理,那么趁着这次总结了下单张或者多张图片上传并且预览的代码:

    <div class="container">
                    <label>请选择一个图像文件:</label>
    
                    <input type="file" accept="image/gif,image/png,image/jpeg,image/jpg" id="file_input" multiple/>
                    <button id="submit">提交</button>
                </div>
                <ul>
                    <li>
                        <div class="pic_cover">
                            <img src="http://www.wangzhibo.top/wp-content/uploads/2018/06/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20180426142248-1-300x300.jpg" alt="证书封皮">
                            <span class="close"></span>
                        </div>
                    </li>
                </ul>

    重点看下input里面的内容  附件上传属性    file      accept  控制上传内容的格式 ,这里是图片 可以 image/*  指所有图片的格式 也可以指定格式的图片如: gif,png,jpeg,jpg,

    multiple   是   控制否单张或者图片的关键   不写就是只能单张上传。

    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script type="text/javascript">
     
     
    window.onload = function(){
        var input = document.getElementById("file_input");
        var result,fd,dataArr = [];
        if(typeof FileReader==='undefined'){
            alert("抱歉,你的浏览器不支持 FileReader");
            input.setAttribute('disabled','disabled');
        }else{
            input.addEventListener('change',readFile,false);
        }
     
     
        function readFile(){
            fd = new FormData();
            var iLen = this.files.length;
            for(var i=0;i<iLen;i++){
                if (!input['value'].match(/.jpg|.gif|.png|.jpeg|.bmp/i)){  //判断上传文件格式
                    return alert("上传的图片格式不正确,请重新选择");
                }
                var reader = new FileReader();
                fd.append(i,this.files[i]);
                reader.readAsDataURL(this.files[i]);  //转成base64
                reader.fileName = this.files[i].name;
     
                reader.onload = function(e){
                    var imgMsg = {
                        name : this.fileName,//获取文件名
                        base64 : this.result   //reader.readAsDataURL方法执行完后,base64数据储存在reader.result里
                    }
                    dataArr.push(imgMsg);
                    result = '<div class="delete">delete</div><div class="result"><img class="subPic" src="'+this.result+'" alt="'+this.fileName+'"/></div>';
                    var div = document.createElement('div');
                    div.innerHTML = result;
                    div['className'] = 'float';
                    document.getElementsByTagName('body')[0].appendChild(div);    //插入dom树
                    var img = div.getElementsByTagName('img')[0];
                    img.onload = function(){
                        var nowHeight = ReSizePic(this); //设置图片大小
                        this.parentNode.style.display = 'block';
                        var oParent = this.parentNode;
                        if(nowHeight){
                            oParent.style.paddingTop = (oParent.offsetHeight - nowHeight)/2 + 'px';
                        }
                    }
                    div.onclick = function(){
                        $(this).remove();                  // 在页面中删除该图片元素
                    }
                }
            }
        }
     
     
        /*function send(){
            var submitArr = [];
            $('.subPic').each(function () {
                    submitArr.push({
                        name: $(this).attr('alt'),
                        base64: $(this).attr('src')
                    });
                }
            );
            $.ajax({
                url : 'http://123.206.89.242:9999',
                type : 'post',
                data : JSON.stringify(submitArr),
                dataType: 'json',
                //processData: false,   用FormData传fd时需有这两项
                //contentType: false,
                success : function(data){
                    console.log('返回的数据:'+JSON.stringify(data))
               }
            })
        }*/
     
     
     
     
        /*oSelect.onclick=function(){
            oInput.value = "";   // 先将oInput值清空,否则选择图片与上次相同时change事件不会触发
            //清空已选图片
            $('.float').remove();
            oInput.click();
        }
     
     
        oAdd.onclick=function(){
            oInput.value = "";   // 先将oInput值清空,否则选择图片与上次相同时change事件不会触发
            oInput.click();
        }
     
     
        oSubmit.onclick=function(){
            if(!dataArr.length){
                return alert('请先选择文件');
            }
            send();
        }*/
    }
    /*
     用ajax发送fd参数时要告诉jQuery不要去处理发送的数据,
     不要去设置Content-Type请求头才可以发送成功,否则会报“Illegal invocation”的错误,
     也就是非法调用,所以要加上“processData: false,contentType: false,”
     * */
     
     
    function ReSizePic(ThisPic) {
        var RePicWidth = 200; //这里修改为您想显示的宽度值
     
        var TrueWidth = ThisPic.width; //图片实际宽度
        var TrueHeight = ThisPic.height; //图片实际高度
     
        if(TrueWidth>TrueHeight){
            //宽大于高
            var reWidth = RePicWidth;
            ThisPic.width = reWidth;
            //垂直居中
            var nowHeight = TrueHeight * (reWidth/TrueWidth);
            return nowHeight;  //将图片修改后的高度返回,供垂直居中用
        }else{
            //宽小于高
            var reHeight = RePicWidth;
            ThisPic.height = reHeight;
        }
    }
     
     

    详细见代码介绍,

    以及简单的css样式 

    .float{
            float:left;
            width : 200px;
            height: 200px;
            overflow: hidden;
            border: 1px solid #CCCCCC;
            border-radius: 10px;
            padding: 5px;
            margin: 5px;
        }
        img{
            position: relative;
        }
        .result{
            width: 200px;
            height: 200px;
            text-align: center;
            box-sizing: border-box;
        }
     
     
        .delete{
            width: 200px;
            height:200px;
            position: absolute;
            text-align: center;
            line-height: 200px;
            z-index: 10;
            font-size: 30px;
            background-color: rgba(255,255,255,0.8);
            color: #777;
            opacity: 0;
            transition-duration: 0.7s;
            -webkit-transition-duration: 0.7s;
        }
     
     
        .delete:hover{
            cursor: pointer;
            opacity: 1;
        }
  • 相关阅读:
    jeecg t:treeSelectTag 联动处理
    saas动态数据源
    jquery ajax超时设置
    创建mysql 数据库脚本
    Java动态创建MySQL数据库
    ant执行sql脚本
    jeecg jeewx 多表查询展示
    @JoinColumn 详解
    hibernate关联映射注解及@JoinColumn的用法
    算发帖——俄罗斯方块覆盖问题一共有多少个解
  • 原文地址:https://www.cnblogs.com/yizhiduxiublog/p/12323959.html
Copyright © 2020-2023  润新知