vue2 上传图片

坚强是说给别人听的谎言 提交于 2020-03-30 06:53:35
<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>

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!