微信小程序 关于头像上传,showActionSheet,chooseImage,uploadFile

本文介绍了微信小程序中实现头像上传的方法,包括使用wx.showActionSheet选择上传方式,wx.chooseImage选取图片,以及wx.uploadFile上传至服务器的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

微信小程序关于头像上传以及相关的几个API(showActionSheet,chooseImage和uploadFile)

近期参与一个微信小程序的开发,有关于上传头像的需求。在实现后,想写下博客记录一下,也希望能帮到有需要的朋友。

效果预览

点击该区域,可以弹出菜单选择
在这里插入图片描述

在这里插入图片描述

js代码

changeAvatarSheet函数

  // About Change Avatar of users
  // 1.换头像的途径菜单 wx.showActionSheet,选择菜单
  // 调用wx.showActionSheet,作选择菜单
  changeAvatarSheet: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#f7982a",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            //从相册中选择
            // console.log(res.tapIndex);
            // 调用函数
            that.chooseWxImageShop('album');
          } else if (res.tapIndex == 1) {
            //手机拍照
            // console.log(res.tapIndex);
            // 调用函数
            that.chooseWxImageShop('camera');
          }
        }
      }
    })
  },
 

chooseWxImageShop函数

  // 2.换头像 wx.chooseImage
  // 3.上传头像到数据库 wx.uploadFile
  chooseWxImageShop: function (type) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          filePath: tempFilePaths[0],
          name: 'avator',
          url: 'url',
          header: {
            'content-type': 'multipart/form-data',
            'X-AUTH-TOKEN': wx.getStorageSync('token')
          }, // 设置请求的 header
          formData: {
            // 'content-type': 'multipart/form-data',
            // 'user_id': '201825010512',
            // 'filePath': tempFilePaths[0],
          },
          success(res) {
            // const data = res.data
            var data = JSON.parse(res.data)
            console.log(res.data)
            //do something
            // that.setData({
            //   app.globalData.userInfo.avatarUrl: data.face_url
            // });
            console.log(data.face_url)
            app.globalData.userInfo.avatarUrl=data.face_url
            console.log(app.globalData.userInfo.avatarUrl)
            wx.showToast({
              title: '上传成功',
            })
          },
          fail: function (res) {
            wx.showToast({
              icon: 'none',
              title: '上传失败',
            })
            console.log(tempFilePaths[0])
          }
        })
      }
    })

  },

经过与后台数据库实测,可以上传更新用户的头像。
主要涉及的API有wx.showActionSheet,wx.chooseImage,wx.uploadFile,分别是弹出选择菜单,选择图像,上传文件。注意函数之间的调用关系。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Silam Lin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值