微信小程序wx.uploadFile的兩個坑


--

setImage:function(e){
    var _this = this  //坑1
    wx.chooseImage({
      count: 1, 
      sizeType: ['original', 'compressed'], 
      sourceType: ['album', 'camera'], 
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        wx.uploadFile({
          url: app.globalData.myhost+'customer-header?access_token='+app.globalData.itoken,
          filePath: tempFilePaths[0],
          name: 'uploadFile',
          formData: {
            'userId': app.globalData.iuserId
          },
          success: function (res) {var data = JSON.parse(res.data)  //坑2:與wx.request不同,wx.uploadFile返回的是[字符串],需要自己轉為JSON格式
            console.log('上傳成功')
            _this.setData({    //坑1:wx.uploadFile本身有一個this,所以要通過外部var _this = this 把this帶進來
              headerImageUrl: data.headerImageUrl
            })
            console.log('刷新成功')
          }
        })
      }
    })
  },

 

 

 

--


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM