zoukankan      html  css  js  c++  java
  • vue上传图片并预览

      1.首先,<input type="file" />的上传附件比较丑,所以我先将附件的样式做了调整,如下:

    这是HTML的代码:

    <div class="fileBox">
                <span class="fileinput-button"> 
                <img :src="srcOthers" alt="" width="100px" height="100px">
                <span>点击上传</span> 
                <input type="file" style=" 100px;height: 100px;" @change="getFileOthers"> 
                </span>
            </div>
            <div class="fileBox-word">
                <span class="fileinput-word">合同及其他扫描件上传</span>
            </div>

    CSS样式代码:

    <style scoped>
    .fileBox{
       100%;
      height: 112px;
      display: flex;
      flex-direction: row;
      justify-content: space-around;
      flex-wrap: wrap;
    }
    .fileBox-word{
         100%;
        height: 30px;
        display: flex;
        flex-direction: row;
        justify-content: space-around;
        flex-wrap: wrap;
    }
    .fileinput-button { 
       100px;
      height: 100px;
      float: left;
      display: flex;
      margin-top: 10px;
      line-height: 150px;
      position: relative; 
      display: inline-block; 
      overflow: hidden; 
      color: #ccc;
      border: 1px solid #ccc;
      background: url(../../assets/img/addUpload.png) no-repeat;
      background-position: center 20px;
    } 
    .fileinput-button input{ 
      position: absolute; 
      left: 0px; 
      top: 0px; 
      opacity: 0; 
      -ms-filter: 'alpha(opacity=0)'; 
    }
    .fileinput-word{
      display: flex;
       100px;
      text-align: center;
      float: left;
      margin-top: 10px;
      position: relative; 
      display: inline-block; 
      overflow: hidden; 
      color: #666;
      font-size: 14px;
    }
    </style>

    最后就是JS代码:

    methods: {
        getFileOthers (e) {//附件预览----合同及其他扫描件
           let _this = this
           var files = e.target.files[0];
           console.log(e.target.files[0]);
           if (!e || !window.FileReader) return  // 看支持不支持FileReader
           let reader = new FileReader()
           reader.readAsDataURL(files) // 这里是最关键的一步,转换就在这里
           reader.onloadend = function () {
             _this.srcOthers = this.result
          }
        },
      },

    这样上传附件并且可以预览就实现了

  • 相关阅读:
    php使用cookie来保存用户登录信息
    Linux下进程操作
    TortoiseSVN无法编辑日志信息的解决方法
    用DIV布局制作公告板
    HashMap的遍历
    实现文本滚动
    子DIV块中设置margintop时影响父DIV块位置的解决办法
    php使用session来保存用户登录信息
    javascript jquery ajax动态提交多个参数 api测试 拂晓风起
    javascript 处理返回json中的\u中文乱码问题(也不是乱码了,就是\u编码) 拂晓风起
  • 原文地址:https://www.cnblogs.com/wxy-developer/p/9449247.html
Copyright © 2011-2022 走看看