• van-uploader上传图片报错Invalid handler for event “load“(在uniapp编译)


    van-uploader使用报错

    原因:主要原因这里使用的vant版本是2.13.0的,在Hbuild里面运行的项目,vant插件在这里会有部分组件有兼容问题,(van-image,van-uploader等)。

    解决:主要是要实现图片上传功能,可以使用uniapp自带的上传组件(uni.uploadFile

    官网地址

    示例:

    <template>
     <scroll-view style="flex: 1">
       <view>
         <view class="uni-padding-wrap uni-common-mt">
           <view class="demo">
             <image
               v-if="imageSrc"
               :src="imageSrc"
               class="image"
               mode="widthFix"
             ></image>
             <text v-else class="uni-hello-addfile" @click="chooseImage"
               >+ 选择图片</text
             >
           </view>
         </view>
       </view>
     </scroll-view>
    </template>
    <script>
    export default {
      data() {
        return {
          imageSrc: '',
          task: null,
        }
      },
      methods: {
        chooseImage: function () {
          uni.chooseImage({
            count: 1,
            success: (res) => {
              console.log('chooseImage success, temp path is', res.tempFilePaths[0])
              var imageSrc = res.tempFilePaths[0]
              uni.showLoading({
                title: '上传中'
              })
              this.task = uni.uploadFile({
                url: 'https://unidemo.dcloud.net.cn/upload', //仅为示例,非真实的接口地址
                filePath: imageSrc,
                name: 'file',
                formData: {
                  'user': 'test'
                },
                //header: {
    	        // "Content-Type": "multipart/form-data",
    	        //Authorization: `${uni.getStorageSync('tokens')}`, // 请求携带token
    	      //},
                success: (res) => {
                  console.log('uploadImage success, res is:', res)
                  uni.hideLoading();
                  uni.showToast({
                    title: '上传成功',
                    icon: 'success',
                    duration: 1000
                  })
                  this.imageSrc = imageSrc
                },
                fail: (err) => {
                  console.log('uploadImage fail', err);
                  uni.hideLoading();
                  uni.showModal({
                    content: err.errMsg,
                    showCancel: false
                  });
                },
              });
            },
            fail: (err) => {
              console.log('chooseImage fail', err)
            }
          })
        },
      }
    }
    </script>
    
    <style>
    .image {
     width: 100%;
    }
    
    .demo {
     background: #fff;
     padding: 50rpx;
     justify-content: center;
     align-items: center;
    }
    
    .uni-hello-addfile {
     text-align: center;
     background: #fff;
     padding: 50rpx;
     margin-top: 10px;
     font-size: 38rpx;
     color: #808080;
    }
    </style>
    
    
    
    • 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
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
  • 相关阅读:
    【客观赋权法1】熵权法(MATLAB全代码)
    网络模块使用Hilt注入
    毫秒时间戳转换为字符串
    【嵌入式】堆栈与单片机内存
    leetcode:2441. 与对应负数同时存在的最大正整数(python3解法)
    树相关——树链剖分(轻重链剖分)
    Leu-Trp-Leu-COOH,42293-99-2
    关于LWIP的一点记录(四)
    C语言之通讯录的实现篇优化版
    开发知识点-stm32/ESP32/Mega2560嵌入式设计
  • 原文地址:https://blog.csdn.net/qq_45331969/article/details/134054928