基于vue、iview单张图片、多张图片上传功能

效果展示:

复制代码立即可用,只需更改data中的图片上传的接口就可以了

1、demo.vue

<template>
  <div class="demo">
    <div class="demo-upload-list" v-if="hasImage">
      <img :src="imageUrl" />
      <div class="demo-upload-list-cover">
        <Icon type="ios-eye-outline" @click.native="handleView(imageUrl)"></Icon>
        <Icon type="ios-trash-outline" @click.native="handleRemove()"></Icon>
      </div>
    </div>
    <Upload
      :action="actionUrl"
      :format="['jpg','jpeg','png']"
      :max-size="2048"
      :on-exceeded-size="handleMaxSize"
      :on-success="handleSuccess"
      :show-upload-list="false"
      style=" width:58px;">
       <Button icon="ios-cloud-upload-outline">上传图片</Button>
    </Upload>

    <div class="demo-upload-list" v-for="(item,index) in imageUrlList" :key="index">
        <img :src="item.url" />
        <div class="demo-upload-list-cover">
          <Icon type="ios-eye-outline" @click.native="handleView(item.url)"></Icon>
          <Icon type="ios-trash-outline" @click.native="handleRemoveList(index)"></Icon>
        </div>
    </div>

    <Upload
      :show-upload-list="false"
      :on-exceeded-size="handleMaxSize"
      :on-success="handleSuccessList"
      :format="['jpg','jpeg','png']"
      :max-size="2048"
      multiple
      type="drag"
      :action="actionUrl"
      style="display: inline-block;width:58px;">
      <div style="width: 58px;height:58px;line-height: 58px;">
        <Icon type="ios-camera" size="20"></Icon>
      </div>
    </Upload>
    <Modal title="图片预览" v-model="visible">
      <img :src="showImageUrl" v-if="visible" style="width: 100%" />
    </Modal>
  </div>
</template>

2、js代码

export default {
  name: 'slideshow',
  data () {
    return {
      actionUrl: '服务器请求地此,上传文件的接口',
      imageUrl: '',
      hasImage: false,
      showImageUrl: '',
      visible: false,
      imageUrlList: [
        {
          url: '***.jpg'
        }
      ]
    }
  },
  methods: {
    handleMaxSize (file) {
      this.$Notice.warning({
        title: '图片大小限制',
        desc: '文件 ' + file.name + '太大,不能超过 2M.'
      })
    },
    upload () {
      this.loadingStatus = true
      setTimeout(() => {
        this.file = null
        this.loadingStatus = false
        this.$Message.success('Success')
      }, 1500)
    },
    handleView (imageUrl) {
      this.showImageUrl = imageUrl
      this.visible = true
    },
    handleRemove () {
      this.imageUrl = ''
      this.hasImage = false
    },
    handleRemoveList (index) {
      // 删除
      this.imageUrlList.splice(index, 1)
    },

    handleSuccessList (res, file) {
      let image = { url: res.data }
      console.log('image:' + image.url)
      this.imageUrlList.push(image)
      console.log(this.imageUrlList)
    },
    handleSuccess (res, file) {
      console.log(res)
      this.imageUrl = res.data
      this.hasImage = true
      console.log(file)
    }
  }
}

3、css

.demo-upload-list {
  display: inline-block;width: 60px;height: 60px;text-align: center;line-height: 60px;
  border: 1px solid transparent;border-radius: 4px;overflow: hidden;background: #fff;
  position: relative;box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);margin-right: 4px;
}
.demo-upload-list img {
  width: 100%;height: 100%;
}
.demo-upload-list-cover {
  display: none;position: absolute;top: 0;bottom: 0;
  left: 0;right: 0;background: rgba(0, 0, 0, 0.6);
}
.demo-upload-list:hover .demo-upload-list-cover {
  display: block;
}
.demo-upload-list-cover i {
  color: #fff;font-size: 20px;cursor: pointer;margin: 0 2px;
}

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值