1. 程式人生 > >微信小程式上傳圖片到7牛雲

微信小程式上傳圖片到7牛雲

封裝上傳的方法

uploadQiniu(a, b) { //上傳圖片的方法

let token = a;  //後臺請求回來的token
let tempFilePaths = b; // 圖片路徑
var that = this;
wx.uploadFile({
  url: 'https://upload-z2.qiniup.com',
  name: 'file',
  filePath: tempFilePaths[0],
  header: {
    "Content-Type": "multipart/form-data"
  },
  formData: {
    token: token,
  },
  success: function (res) {
    let data = JSON.parse(res.data)
    that.data.imgSrc = "https://pub.qinius.butongtech.com" + "/" + data.key //地址拼接
    that.setData({
      imgSrc: that.data.imgSrc  
    }) 
  },

})

}

上傳的事件

headPortrait() { //上傳照片
let that = this
wx.chooseImage({
count: 1,
sizeType: [‘original’, ‘compressed’],
sourceType: [‘album’, ‘camera’],
success(res) {
// tempFilePath可以作為img標籤的src屬性顯示圖片
const tempFilePaths = res.tempFilePaths
that.setData({
imgSrc: tempFilePaths[0]
})
that.uploadQiniu(that.data.qiniuToken,tempFilePaths); // 上傳圖片到七牛的方法

  }
})

},