微信小程序上传图片到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); // 上传图片到七牛的方法

  }
})

},

猜你喜欢

转载自blog.csdn.net/m18565890306/article/details/83930006