• 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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84

    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;
        }
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  • 相关阅读:
    JavaCV音视频开发宝典:UDP广播推流 使用UDP方式推送广播TS流 实现UDP一对多广播
    FinalShell软件连接成功后,root文件夹显示一直加载中....
    15 -- 最接近原点的 K 个点
    数据分析方法-对比分析和用户画像(文末送书)
    MySQL数据库基础知识(二)
    分类树,我从2s优化到0.1s
    微信撤回时间延长至3小时,真的?
    【机器学习】LoRA:大语言模型中低秩自适应分析
    PDF 文件与其他文档格式相比有哪些优势?
    Aria2 for Mac (免HomeBrew)
  • 原文地址:https://blog.csdn.net/qq_43923146/article/details/133904286