微信小程序之上传图片到云端存储并返回图片地址

让用户选择照片并上传到小程序云端存储空间的代码

1、原理

1)wx.cloud.uploadFile()

upload

2)wx.chooseImage()choose imagetype

2、整合代码

//JS文件   上传图片函数
upload_picture: function(name) {
    var that = this
    //让用户选择或拍摄一张照片
    wx.chooseImage({
      count: 1,	
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
      //选择完成会先返回一个临时地址保存备用
        const tempFilePaths = res.tempFilePaths
        //将照片上传至云端需要刚才存储的临时地址
        wx.cloud.uploadFile({
          cloudPath: 'test.jpg',
          filePath: tempFilePaths[0],
          success(res) {
          //上传成功后会返回永久地址
           	console.log(res.fileID) 
          }
        })
      }
    })
  }
3、实例体验

Alt

发布了13 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/downanddusk/article/details/88385734