zoukankan      html  css  js  c++  java
  • vue2 上传图片

    <template>
      <div class="vue-upload-img-multiple">
        <div v-if="images.length >0">
          <ul>
            <li v-for="image in images">
              <img :src="image" @click='delImage($index)' />
              <a href="#" style="position: absolute;" @click='delImage($index)'>
                <span class="glyphicon glyphicon-remove"></span>
              </a>
            </li>
          </ul>
          <button @click="removeImage">移除全部图片</button>
          <button @click='addPic' >上传</button>
        </div>
    
        <div>
          <div v-if="!image">
            <h2>Select an image</h2>
            <input type="file" @change="onFileChange">
          </div>
          <div v-else>
            <img :src="image" />
            <button @click="removeImage">Remove image</button>
          </div>
        </div>
    
      </div>
    </template>
    
    <script>
    export default {
      name: 'Upload',
      data: function () {
        return {
          images: []
        }
      },
      methods: {
        test () {
          var vm = this
          console.log(vm.message)
        },
        addPic () {
          $('input[type=file]').trigger('click')
          return false
        },
        onFileChange (e) {
          var files = e.target.files || e.dataTransfer.files
          if (!files.length) return
          this.createImage(files)
        },
        createImage (file) {
          var vm = this
          var reader = null
          var leng = file.length
          for (var i = 0; i < leng; i++) {
            reader = new window.FileReader()
            reader.readAsDataURL(file[i])
            reader.onload = function (e) {
              vm.images.push(e.target.result)
            }
          }
        },
        removeImage: function (e) {
          this.images = []
        },
        delImage: function (index) {
          this.images.shift(index)
        }
      }
    }
    </script>
    
    <style lang="scss">
    h1, h2 {
      font-weight: normal;
    }
    
    ul {
      list-style-type: none;
      padding: 0;
    }
    
    li {
      display: inline-block;
      margin: 0 10px;
    }
    
    a {
      color: #42b983;
    }
    .vue-upload-img-multiple{
      border:1px red solid;
    }
    </style>
  • 相关阅读:
    中文词语的语法划分
    程序员转行可以做什么?
    Linux Crontab 定时任务 命令详解
    Spring对AOP的支持
    ASP.NET 2.0 Web Part编程入门
    linux ramdom hung up
    VLC plugin加载代码分析
    mac上的ssh proxy客户端 iSSH个人修改版
    关于MP4 fileformat中 duration及timescale相关的几个地方
    inline 小结
  • 原文地址:https://www.cnblogs.com/ssrsblogs/p/6101479.html
Copyright © 2011-2022 走看看