• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

vue实现上传图片

武飞扬头像
程序小菜鸟·
帮助1

1.实现效果

学新通

2.input标签的属性

  • multiple:是否多选
    <input type="file"  multiple="multiple"  accept="image/*" /> 
    
  • accept:文件格式,这个可以控制文件格式,比如jpeg和gif还有Png之类的
    <input type="file"  multiple="multiple"  accept="image/png,image/gif,image/jpeg"  /> 
    
  • capture表示,可以捕获到系统默认的设备,比如:camera照相机;camcorder摄像机;microphone录音
    <input type="file"  multiple="multiple"  accept="image/png,image/gif,image/jpeg"  capture="camera"  /> 
    

 3.实现代码

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
    <head>
  4.  
    <meta charset="UTF-8">
  5.  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.  
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7.  
    <title>上传文件</title>
  8.  
    </head>
  9.  
    <style>
  10.  
    body,*{
  11.  
    -webkit-tap-highlight-color:transparent;
  12.  
    }
  13.  
     
  14.  
    .add_box{
  15.  
    width: 100px;
  16.  
    height: 100px;
  17.  
    border: 1px solid #ccc;
  18.  
    border-radius: 10px;
  19.  
    position: relative;
  20.  
    font-size: 8px;
  21.  
    text-align: center;
  22.  
    margin: 40px 20px;
  23.  
    padding: 5px;
  24.  
    }
  25.  
     
  26.  
    .flex{
  27.  
    display: flex;
  28.  
    align-items: center;
  29.  
    }
  30.  
    .add_icon{
  31.  
    position: absolute;
  32.  
    font-size: 50px;
  33.  
    left: 50%;
  34.  
    top: 50%;
  35.  
    transform: translate(-50%,-50%);
  36.  
    }
  37.  
    .i_none{
  38.  
    left: 0;
  39.  
    top: 0;
  40.  
    position: absolute;
  41.  
    width: 100%;
  42.  
    height: 100%;
  43.  
    overflow: hidden;
  44.  
    cursor: pointer;
  45.  
    opacity: 0;
  46.  
    }
  47.  
    .upload_box{
  48.  
    display: flex;
  49.  
    justify-content: center;
  50.  
    align-items: center;
  51.  
    flex-wrap: wrap;
  52.  
    }
  53.  
    .da_img{
  54.  
    width: 100%;
  55.  
    height: 100%;
  56.  
    object-fit: cover;
  57.  
    }
  58.  
    .deleBtn{
  59.  
    width: 100px;
  60.  
    height:100px;
  61.  
    position: absolute;
  62.  
    text-align: center;
  63.  
    line-height: 100px;
  64.  
    z-index: 10;
  65.  
    font-size: 10px;
  66.  
    background-color: rgba(255,255,255,0.8);
  67.  
    color: #777;
  68.  
    opacity: 0;
  69.  
    transition-duration: :0.7s;
  70.  
    -webkit-transition-duration: 0.7s;
  71.  
    cursor: default;
  72.  
    }
  73.  
    .deleBtn:hover{
  74.  
    opacity: 1;
  75.  
    }
  76.  
    </style>
  77.  
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  78.  
    <body>
  79.  
    <div id="app">
  80.  
     
  81.  
    <div class="upload_box">
  82.  
    <div class="add_box" v-show="showAdd" >
  83.  
    <div class="add_icon"> </div>
  84.  
    <div>上传图片(最多5张)</div>
  85.  
    <form action="" enctype="multipart/form-data">
  86.  
    <input type="file" multiple="multiple" accept="image/*" class="i_none" id="i0" @change="getImg" />
  87.  
    </form>
  88.  
    </div>
  89.  
    <div class="add_box flex" v-for="(item,index) in img_list" :key="index" >
  90.  
    <div class="deleBtn" @click="del(index)">删除</div>
  91.  
    <img class="da_img" :src="https://blog.csdn.net/aaa123aaasqw/article/details/item" />
  92.  
    </div>
  93.  
    </div>
  94.  
    </div>
  95.  
    <script>
  96.  
    let vue=new Vue({
  97.  
    el:"#app",
  98.  
    data:{
  99.  
    img_list:[],
  100.  
    showAdd:true,
  101.  
    },
  102.  
     
  103.  
    methods:{
  104.  
    getImg(e){
  105.  
    var file = e.target.files;
  106.  
    // let oneUploadLen=file.length;
  107.  
    // if(oneUploadLen>5){
  108.  
    // return alert("最多上传5张图片");
  109.  
    // }
  110.  
    for(var i = 0;i<file.length;i ){
  111.  
    let fileReader=new FileReader();
  112.  
    fileReader.readAsDataURL(file[i]);
  113.  
    //readAsDataURL方法将图片转为base64格式存储于result中
  114.  
    let imgSize=file[i].size;
  115.  
    if(imgSize>1024*1024*1){
  116.  
    return alert("上传图片不能超过1M");
  117.  
    }
  118.  
    if(file[i].type != 'image/png' && file[i].type != 'image/jpeg' && file[i].type != 'image/gif'){
  119.  
    return alert("图片上传格式不正确");
  120.  
    }
  121.  
    fileReader.onload=((res)=>{//encodeURIComponent
  122.  
    let img=res.srcElement.result;
  123.  
    if(this.img_list.length<4){
  124.  
    this.img_list.push(img);
  125.  
    }else if(this.img_list.length==4){
  126.  
    this.img_list.push(img);
  127.  
    this.showAdd=false;
  128.  
    }
  129.  
    })
  130.  
    // 上传失败
  131.  
    fileReader.onerror=((res)=>{
  132.  
    console.log(res)
  133.  
    })
  134.  
    }
  135.  
    },
  136.  
    del(index){
  137.  
    this.img_list.splice(index,1)
  138.  
    }
  139.  
    },
  140.  
     
  141.  
    mounted() {
  142.  
     
  143.  
    },
  144.  
    created() {
  145.  
     
  146.  
    },
  147.  
    })
  148.  
     
  149.  
    </script>
  150.  
    </body>
  151.  
    </html>
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhhkbckf
系列文章
更多 icon
同类精品
更多 icon
继续加载