云储存成功回调更新页面数据失败
发布于 4 年前 作者 guiyingtao 1921 次浏览 来自 问答
doUpload: function () {
    var that = this;
    // 选择图片
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.showLoading({
          title: '上传中',
        })
        const filePath = res.tempFilePaths[0]
        var fileName = filePath[11] + filePath[12] + filePath[59] + filePath[60] + filePath[101] + filePath[102]
        const cloudPath = fileName + filePath.match(/\.[^.]+?$/)[0]
        // 上传图片
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: res => {
            that.setDate({
              vsimgurl: res.fileID
            })
            console.log('[上传文件] 成功:', res)
          },
          fail: e => {
            console.error('[上传文件] 失败:', e)
            wx.showToast({
              icon: 'none',
              title: '上传失败',
            })
          },
          complete: () => {
            wx.hideLoading()
          }
        })
      },
      fail: e => {
        console.error(e)
      }
    })
  },

上面是我的代码

主要是上传文件成功后回调,需要更新页面图片路径的数据,一直失败,不懂为啥

求大神解答

3 回复

文件是上传成功了 但代码运行到setDate就不动了,也没报错

应该是uploadFile中的success回调中的this指向出错了,用在回调之前保存一下this 如const self = this; 回调是时候用self.setState();

所以错误信息呢

回到顶部