小程序上传图片

本文介绍如何在小程序中使用wx.chooseImage接口进行图片选取并上传的流程,帮助开发者实现小程序内的图片上传功能。

小程序上传图片

wx.chooseImage

<button bindtap="upload">上传图片</button>
    //上传图片
    upload(){
      // 选择图片
      wx.chooseImage({
        count: 1,
        sizeType: ['compressed'],
        sourceType: ['album', 'camera'],
        success: function (res) {
          wx.showLoading({
            title: '上传中',
          })
          const filePath = res.tempFilePaths[0]  //324324.png
          // 上传图片
          const cloudPath = 'my-image' + filePath.match(/\.[^.]+?$/)[0]  //上传路径
          wx.cloud.uploadFile({ //上传图片到云存储
            cloudPath,  //云存储路径
            filePath,   //本地图片路径
            success: res => {
              console.log('[上传文件] 成功:', res)
            },
            fail: e => {
              console.error('[上传文件] 失败:', e)
              wx.showToast({
                icon: 'none',
                title: '上传失败',
              })
            },
            complete: () => {
              wx.hideLoading()
            }
          })
        },
        fail: e => {
          console.error(e)
        }
      })
    }
filePath.match(/\.[^.]+?$/)[0]  这一部分是截取图片  类似这样格式324324.png截取成.png格式
'my-image' + filePath.match(/\.[^.]+?$/)[0]  然后拼接成这样的形式  my-image.png

了解更多点击下方
wx.chooseImage

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值