uniapp vue3.0+TS 上传单张或多张图片,并且能删除和预览。

一、uniapp vue3.0+TS 上传单张或多张图片,并且能删除和预览。

效果:人菜话不多 先上效果:
在这里插入图片描述

二、代码

1.HTML 上传图片相关代码

代码如下:

<template>
  <view class="images_box">
       <view class="imgBox" v-for="(item, index) in fileList" :key="index">
         <img class="image" :src="item.url" alt="" @click="previewImg(index)" />
         <img @click.stop="delImges(item, index)" class="close" src="@/static/image/base/close.png" alt="删除" />
       </view>
       <view class="iconBox" @click="upload">
         <u-icon name="plus" size="28" color="#438BEF"></u-icon>
       </view>
     </view>
</template>

2.TS上传图片相关代码

代码如下:

<script setup lang="ts">
import { ref, reactive } from "vue";
import { onLoad } from "@dcloudio/uni-app";
import { examine } from "@/api";

const fileList = ref<any>([]);
const imgList = ref<any>([]);
const formState = reactive<any>({
  conclusion: "", 
  suggestion: "", 
  task_id: null
});

const upload = () => {
  uni.chooseImage({
    count: 9, // 默认9
    mediaType: ["image"],
    sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图,默认二者都有
    sourceType: ["album", "camera"], // 可以指定来源是相册还是相机,默认二者都有
    success: function (res: any) {
      fileList.value = fileList.value.concat(res.tempFiles); //concat追加到数组
      for (let file in fileList.value) {
        up_img(fileList.value[file]);
      }
    },
  });
};
const up_img = (file: any) => {
  uni.uploadFile({
    url: 'https://prod.XXX.com/XXXXX/XXX/XXXX/',
    filePath: file.path,
    name: 'files',
    header: {
      "Content-Type": "multipart/form-data"
    },
    success: (res: any) => {
      let obj = JSON.parse(res.data);
      imgList.value = [
        ...imgList.value,
        {
          type: obj.files[0].type,
          size: obj.files[0].size,
          name: obj.files[0].name,
          url: 'https://prod.XXXXXX/XXXX/XXXX/XXXXX/' + obj.files[0].name,
        }
      ];
      fileList.value = imgList.value
      uni.showToast({
        title: '上传成功',
        icon: "success",
      });
    },
    fail: (res: any) => {
      uni.showToast({
        title: '上传失败',
        icon: "error",
      });
    }
  })
};
//删除图片
const delImges = async (rowItem: any, index: number) => {
  fileList.value.forEach((item: any) => {
    if (item.name == rowItem.name) {
      fileList.value.splice(index, 1)
    }
  })
};
// 点击图片预览
const previewImg = (index: number) => {
  let imgsArray = fileList.value.map((item: any) => {
    return item.url
  })
  uni.previewImage({
    urls: imgsArray,
    current: index,
    indicator: 'number',
    loop: true
  })
}
onLoad((options: any) => {
  formState.task_id = Number(options.taskId)
});
</script>

3.CSS 上传图片相关代码

代码如下:

 .images_box {
   display: flex;
    justify-items: flex-start;
    flex-wrap: wrap;

   .imgBox {
     width: 120rpx;
     height: 120rpx;
     position: relative;
     margin: 20rpx 20rpx 0 0;

     .image {
       width: 100%;
       height: 100%;
        border-radius: 10rpx;
      }

      .close {
        width: 30rpx;
        height: 30rpx;
        position: absolute;
        top: -15rpx;
        right: -15rpx;
      }
    }

    .iconBox {
      width: 120rpx;
      height: 120rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 10rpx;
      border: 1px dashed gray;
      box-sizing: border-box;
      margin-top: 20rpx;
    }
  }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值