vue项目vant图片上传总结

本文总结了在Vue项目中使用Vant进行图片显示、上传前校验、上传接口调用、图片删除以及点击缩略图预览原图等关键步骤。针对图片显示不正常的问题,提出了解决方案——为每个图片对象添加isImage: true属性。同时,介绍了如何在上传组件上设置:preview-full-image="false"和@click-preview="clickPreview"以实现预览功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

页面

    <!-- 上传照片 -->
    <van-field name="uploader">
      <template #input>
        <van-uploader
          ref="uploadImg"
          v-model="images"
          :before-delete="beforeDel"
          :before-read="beforeRead"
          :after-read="afterRead"
          multiple
          :preview-full-image="false"
          @click-preview="clickPreview"
          preview-cover="加载中..."
          accept="image/jpg,image/jpeg"
          :max-size="5 * 1024 * 1024">
          </van-uploader>
      </template>
    </van-field>
    <!-- 加载提示 -->
    <van-overlay :show="showPopup">
      <div class="wrapper" @click.stop style="margin-top: 200px;">
        <van-loading size="80px" color="#ffffff" vertical >加载中...</van-loading>
      </div>
    </van-overlay>
    <!-- 原图预览 -->
    <van-image-preview v-model="show" :images="previewOptions.images" :closeable="true" :show-index="false">
    </van-image-preview>

图片显示不正常

每个图片对象加上 isImage: true

// 向后台获取图片列表数据
getImg(upData).then(response => {
          this.images.push({
            url: 'data:image/jpeg;base64,' + response.data,
            isImage: true
          })
        }).catch(err => {
          console.log(err)
        })

图片上传前校验及上传调用接口

    <!-- 上传照片 -->
    <van-field name="uploader">
      <template #input>
        <van-uploader
          v-model="images"
          @oversize="onOversize"
          :before-read="beforeRead"
          :after-read="afterRead"
          </van-uploader>
      </template>
    </van-field>
    
    methods:{
    //图片上传前校验格式大小问题
    beforeRead (file) {
    // 多张图片
      if (file instanceof Array) {
        for (let i = 0; i < file.length; i++) {
          if (file[i].type !== 'image/jpeg' && file[i].type !== 'image/jpg') {
            this.$toast.fail('请上传 jpg、jpeg 格式图片')
            return false
          }
          return true
        }
      } else {
       // 单张图片
        if (file.type !== 'image/jpeg' && file.type !== 'image/jpg') {
          this.$toast.fail('请上传 jpg、jpeg 格式图片')
          return false
        }
        return true
      }
    },
    afterRead (file) {
    // 此时可以自行将文件上传至服务器
      if (file instanceof Array) {
        file.map((v) => {
          v.status = 'uploading'
          v.message = '上传中...'
          this.uploadImg(v)
        })
      } else {
        file.status = 'uploading'
        file.message = '上传中...'
        this.uploadImg(file)
      }
    },
    // 上传
    uploadImg (file) {
      const param = new FormData()
      param.append('file', file.file)
      // 额外需要添加的参数,可以添加到param中
      const that = this
      upload(param).then((res) => {
        if (res.status === '1') {
          if (file instanceof Array) {
            // 判断是否是数组
            file.map((v, i) => {
              v.status = 'success'
              v.message = ''
            })
          } else {
            file.status = 'success'
            file.message = ''
          }
        } else {
          file.status = 'failed'
          file.message = '上传失败'
          that.$toast(res.data.errMsg)
        }
      }).catch((err) => {
        console.log(err)
        this.$toast('上传失败!')
      })
    }
}

图片删除调用接口

    async beforeDel (elIndex) {
      const isDel = await Dialog.confirm({
        message: '确定删除吗?'
      })
      if (isDel === 'confirm') {
          const vm = this
          // 请求后台接口进行删除,根据后台返回的不同状态进行处理
          // 删除成功后
          this.$toast.success('删除成功!')
          // 将预览图片清空,不然容易导致删除后,预览原图不对应问题
          this.clickedImages = new Array(this.photos.length)
          this.previewOptions.images = []
          // name.index代表图片的索引
          return (file, name) => {
            const fileIndex = name.index
            // console.log(fileIndex)
            vm.fileList[elIndex].splice(fileIndex, 1)
            vm.fileListIs[elIndex].splice(fileIndex, 1)
          }
      }
    },

点击缩略图预览原图问题

首先上传组件加上这两个
:preview-full-image=“false”
@click-preview=“clickPreview”

    async clickPreview (file, name) {
      // 如果原图预览列表中不存在这张缩略图对应的索引,请求后台获取原图
      if (!this.previewOptions.images[name.index]) {
        this.showPopup = true
        const upData = {}
        getImg(upData).then(response => {
          this.previewOptions.images[name.index] = `data:image/jpeg;base64,${response.data}`
          this.showPopup = false
          this.show = true
          this.index = name.index
        }).catch(err => {
          this.showPopup = false
          this.show = false
          console.log(err)
        })
      } else {
        this.show = true
        this.index = name.index
      }
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值