uniapp实现图片上传功能

效果图:

 html:

<view class="upload_info">
  <view class="upload_item" v-for="(item, index) of gallery" :key="index">
    <image :src="item" mode="aspectFill"></image>
    <text class="delete" @click="onDeleteImg(index)"></text>
  </view>
  <view
    class="upload_item upload_img"
    @click="onImgsUpload"
    v-if="gallery.length < 8"
  >
    +
  </view>
</view>

css:

.upload_info {
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
  margin-top: 18rpx;
  margin-bottom: 18rpx;
  .upload_item {
    margin-top: 16rpx;
    margin-right: 16rpx;
    position: relative;
    width: 190rpx;
    height: 190rpx;
    display: flex;
    justify-content: center;
    background: #fff;
    font-size: 100rpx;
    line-height: 170rpx;
    color: #cccccc;
    image {
      width: 100%;
      height: 100%;
    }
    .delete {
      position: absolute;
      top: 0;
      right: 0;
      transform: translate(25%, -25%);
      width: 36rpx;
      height: 36rpx;
      background: url(@/static/icon/ic_delete.png) center/cover no-repeat;
    }
    &.upload_img {
      border: 2rpx dashed #e5e5e5;
    }
  }
}

js:

//上传图片
    onImgsUpload() {
      uni.chooseImage({
        count: 1,
        success: (res) => {
          res.tempFilePaths.forEach((item) => {
            uni.uploadFile({
              url: this.uploadUrl,
              header: {
                Authorization: "Bearer " + this.userInfo.token,
              },
              filePath: item,
              name: "file",
              success: function (uploadFileRes) {
                this.picUrls = [];
                this.picUrls.push(JSON.parse(uploadFileRes.data).data.url);
                this.gallery = this.gallery.concat(this.picUrls);
              },
            });
          });
        },
      });
    },
// 删除图片
    onDeleteImg(index) {
      this.gallery.splice(index, 1);
      this.picUrls.splice(index, 1);
    },

结语:功能比较简单,方便以后粘贴使用,欢迎大家补充交流。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值