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

van-uploader使用报错

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

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

官网地址

示例:

javascript 复制代码
<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>
相关推荐
喜欢吃燃面3 分钟前
Linux 进程信号深度解析:从概念到产生机制
linux·开发语言·学习
AI玫瑰助手4 分钟前
Python基础:字符串的常用内置方法(查找替换分割)
android·开发语言·python
Foreer黑爷12 分钟前
Java并发工具箱:CountDownLatch与CyclicBarrier使用指南
java·开发语言·jvm
syker17 分钟前
AIFerric v2.0 项目总结报告
c语言·开发语言·c++
qq_120840937120 分钟前
Three.js 工程向:相机控制与交互手感调优(OrbitControls)
前端·javascript·orbitcontrols
2501_9159214321 分钟前
穿越HTTPS迷雾:Wireshark中的TLS抓包秘诀与文件合并方法
网络协议·ios·小程序·https·uni-app·wireshark·iphone
周杰伦fans29 分钟前
C# CAD二次开发:RotatedDimension 文字边框设置完全指南
开发语言·c#
蚰蜒螟40 分钟前
从 pthread_create 到 thread_native_entry:glibc 如何唤醒 Java 线程
java·开发语言
We་ct43 分钟前
LeetCode 300. 最长递增子序列:两种解法从入门到优化
开发语言·前端·javascript·算法·leetcode·typescript
gCode Teacher 格码致知44 分钟前
Python提高: unittest和 pytest的使用方法-由Deepseek产生
开发语言·python·pytest