• 工作笔记——上传图片


    html结构

    <div class="col-xs-12" id="product_image">
                  <div class="base-img-box">
                      
                  </div>
                  <div class="img-box">
                      <div class="add-img">
                           +
                      </div>
                      <input type="hidden" name="imgLen">
                      <input type="file" name="file"  class="img-input" id="img-input1"/>    
                  </div>
             </div> 
    

    js

    function UploadImg(context,fileInput){
            var that = this;
                that.context = context;
                that.fileInput = fileInput;
                that.init();
           };
           UploadImg.prototype = {
    
                init:function(){
                    var that = this;
                    $(that.context).find('.add-img').click(function(){
                        $(that.context).find(that.fileInput).eq(0).trigger('click');                            
                    })
    
                    that.readFile();
                    that.reviewImg();
                },
                readFile:function(){
                    var that = this;
                    var fileInput =$(that.context).find(that.fileInput)[0];
                    $(fileInput).on('change',function(){
                        that.showFile();
                   })
    
                },
                showFile:function(){
                    var that = this;
                    var thisInput = $(that.context).find(that.fileInput)[0];
                    var file = thisInput.files[0];
                    if(!/image/w+/.test(file.type)){
                        alert('请上传图片');
                        return false;
                    }
                    var reader = new FileReader();
                        reader.readAsDataURL(file);
                        reader.onload = function(){                                                       
                            var str = '<div class="img-box font-none"><img src="'+this.result+'" width="150" height="150"></img><input name="" type="hidden"  value="'+this.result+'"><i class="remove-img">X</i></div>';
                                $(that.context).find('.base-img-box').append(str);                                        
                                that.removeImg();
                                that.renameInput();
                                                               
                        }
                },
                removeImg:function(){
                    var that = this;
                    $(that.context).find('.base-img-box .remove-img').on('click',function(){                            
                        $(this).parents('.img-box').remove(); 
                        that.renameInput();                                                      
                    })
                },
                renameInput:function(){
                    var that = this;
                    var inputLen =$(that.context).find('.base-img-box .img-box').length;
                        $(that.context).find('[name="imgLen"]').val($(that.context).find('.base-img-box .img-box').length); 
                        $.each($(that.context).find('.base-img-box .img-box'),function(i,item){
                            var index = i+1;
                            $(item).find('input').attr('name','file'+index);
                        })                                                           
                },
                reviewImg:function(){
                	var that = this;
                	var vl = $('.vl');
                    var html = '';
                    $.each(vl,function(index,item){
                 	    html+='<div class="img-box font-none">';
                        html+= '<input type="hidden" name="" value="'+$(item).val()+'"/><img src="data:image/png;base64,'+$(item).val()+'" width="150" height="150"/>'
                        html+='<i class="remove-img">X</i></div>';
                    })
                    $('.base-img-box').append(html);
                    that.renameInput();
                    that.removeImg();
                }
           };
    
    
           new UploadImg('#product_image','#img-input1'); 
    

      

    css

    /*上传图片*/
    .img-box{
         float:left;
         position:relative;
         150px;
         height:150px;
         margin-right:15px;;
         line-height:150px;
         text-align:center;
         font-size:60px;
         font-weight:800;
         border:1px solid #e5e5e5;
         cursor:pointer;
         overflow:hidden;
         
    }
    .img-box.font-none{
        font-size:0;
    }
    .img-box img{
        150px;
        height:150px;
    }
    .img-input{
         display:none;
         visibility: hidden; 
         position: absolute;
    }
    .remove-img{
        position: absolute;
        display: block;
        30px;
        height:30px;
        top:10px;
        right:10px;
        font-size:20px;
        font-weight: 800;
        line-height: 30px;
        text-align: center;
        font-style: normal;
        color:#fff;
        background-color: rgba(154,7,7,.5);
        border-radius: 100%
    }
    .btn-xs{
    	float:right;
    }
    /*结束 上传图片*/
    

    效果图

    自己写的比较简陋的demo,各位看官手下留情  

  • 相关阅读:
    【笑话】程序员和青蛙公主
    C#获取实体类属性名和值 | 遍历类对象
    VS2010 C# 使用DirectSound
    Serializing CTreeCtrl to / from a text file
    DirectX编程:[初级]C# 中利用 DirectSound 录音
    Mixed mode assembly is built against version 'v1.1.4322' of the runtime and...问题——C# DirectXSound
    DirectX编程:[初级]C#中利用DirectSound播放WAV格式声音[最少只要4句话]
    C#中使用DirectSound录音
    官网下载Google Chrome离线安装包
    设计模式读书笔记[1]:策略模式(Strategy)
  • 原文地址:https://www.cnblogs.com/MonaSong/p/5942285.html
Copyright © 2020-2023  润新知