云存储上传本地图片+云数据库存储

云存储上传本地图片

第一步

<button type="primary" bindtap="addmall">上传图片</button>

第二步

addmall: function() {
    // 从本地相册选择图片或使用相机拍照
    wx.chooseImage({
      count:1,
      success: function(res) {
        //  获取本地地址
        const filePath = res.tempFilePaths[0]
        // 格式化
        const tempfile = filePath.split('.')
        // 云函数地址 'my-img'
        const cloudPath = 'my-img-' + tempfile[tempfile.length-2]
        //  上传图片
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: function (res) {
            console.log(res)
            wx.showToast({
              title: '上传成功',
            })
          }
        })
      }

上传图片并添加到云数据库中

addmall: function() {
    // 从本地相册选择图片或使用相机拍照
    wx.chooseImage({
      count:1,
      success: function(res) {
        //  获取本地地址
        const filePath = res.tempFilePaths[0]
        // 格式化
        const tempfile = filePath.split('.')
        // 云函数地址 'my-img.'自定义,此处加点
        const cloudPath = 'my-img-' + tempfile[tempfile.length-2]
        //  上传图片
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: function (res) {
            db.collection('emall').add({
              data: {
                title: '商品1',
                pricce: 23,
                imgUrl: res.fileID
              },
              success: function (res) {
                console.log(res)
                wx.showToast({
                  title: '新增成功',
                })
              }
            })
          }
        })
      },
    })
  }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。