uniapp结合uview实现图片批量上传

uview中上传图片组件的代码

详细请看uview官网,本次用的事件有两个
在这里插入图片描述在这里插入图片描述

1、模板中的组件使用

 <u-upload
        ref="uUploadMultiple"
        :file-list="uploadImgs"
        accept="image"
        :max-size="4 * 1024 * 1024"
        @oversize="oversize"
        :show-progress="false"
        :auto-upload="false"
        max-count="4"
        width="180"
        height="180"
        @on-remove="deletePic"
        @on-choose-complete="afterReadImgs"
      >
      </u-upload>

2、data中声明的数据

  data () {
    return {
      uploadImgs: [],
      activityForm: {
        activityPicInfos: [], 
      },
      header: {
        //根据你接口需求自定义
        Authentication: this.token,
      },
      uploadImgUrl,//图片上传地址
    }
  },

3、method中的代码逻辑

    // 文件超出大小限制
    oversize () {
      uni.showToast({
        title: '图片最大不能超过4M',
        icon: 'none',
      })
    },
    // 读取文件 多张图片上传
    afterReadImgs (event) {
      this.multipleUpload(this.$refs.uUploadMultiple.lists, 2)
    },
    // 多张图片上传
    multipleUpload (event, type) {
      let that = this
      let num = 5
      event.map(item => {
        if (num === event.length) {
          uni.showToast({
            title: '最多上传4张图片',
            icon: 'none',
          })
          return
        }
        num += 1
        if (item.file) {
          that.uploadDo(item, type)
        }
      })
    },
    // 执行上传
    uploadDo (event, type) {
      const that = this
      uni.uploadFile({
        // source 上传图片来源 0:用户上传头像:1:活动 2:攻略 3:酒店 4:景点(0、1、2时需要传token)
        url: uploadImgUrl + '?source=1',
        filePath: event.file.path,
        header: {
          Authentication: that.token,
        },
        name: 'file',
        success (res) {
          const { data } = JSON.parse(res.data)
          that.activityForm.activityPicInfos.push({
            picUrl: data.fileUrl,
            isFirst: 0,
          })
        },
      })
    },
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值