上传图片并保存至云存储再存入数据库,并实时显示图片

  1. 云开发控制台的数据库里创建好集合articles
  2. index.wxml:
//这里处理为一个轮播图,实时轮播出artcles数据库中的图片
<swiper indicator-dots="{{indicatorDots}}" 
  autoplay="{{autoplay}}" interval="{{interval}}" 
  duration="{{duration}}">
    <block wx:for="{{imgUrls}}">
      <swiper-item>
        <image src="{{item}}" class="slide-image" width="100%" height="100%" />
      </swiper-item>
    </block>
  </swiper>
  <!-- 上传图片 -->
  <view class="uploader">
    <view class="uploader-text" bindtap="doUpload">
      <text>上传图片</text>
    </view>
    <view class="uploader-container" wx:if="{{imgUrl}}">
      <image class="uploader-image" src="{{imgUrl}}" mode="aspectFit" bindtap="previewImg"></image>
    </view>
  </view>
  1. index.js:
data:{
     userInfo: {},
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    imgUrls: [],
    openid: ''
},
onload: function(){
 		const that = this
      const db = wx.cloud.database()
      // 查询当前用户所有的 articles
      let _arr = []
      db.collection('articles').get({
        success: res => {
          var arr = res.data;
          for (var i = 0; i < arr.length; i++) {
            _arr.push(
              arr[i].bigImg
            )

          }
          this.setData({
            imgUrls: _arr
            })
          console.log('[数据库] [查询记录] 成功: ', res.data)
            
        },
        fail: err => {
          wx.showToast({
            icon: 'none',
            title: '查询记录失败'
          })
          console.error('[数据库] [查询记录] 失败:', err)
        }
      })
},
// 上传图片
  doUpload: function () {
    let _this = this;
    // 选择图片
    wx.chooseImage({
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.showLoading({
          title: '上传中',
        })

        const filePath = res.tempFilePaths[0]
        console.log(filePath )

		//给上传图片设置好存储路径
        const name = Math.random() * 1000000;
        console.log(name) 
        const cloudPath = name + filePath.match(/\.[^.]+?$/)[0] //存储路径名
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: res => {
            let fileID = res.fileID;
            console.log('[上传文件] 成功:', fileID)
            

			//将成功上传的图片存入artcles数据库中
            const db = wx.cloud.database();
		
            db.collection("articles").add({
              data: {
                bigImg: fileID
              },
             
              success: function () {
                //添加成功后更新imgurls,即能够在页面中显示新添加的图片
                _this.data.imgUrls.push(fileID);
                _this.setData({     imgUrls: _this.data.imgUrls
                });
               
                wx.showToast({
                  title: '图片存储成功',
                  'icon': 'none',
                  duration: 3000
                }),
                  console.log("255");
              },
              fail: function () {
                wx.showToast({
                  title: '图片存储失败',
                  'icon': 'none',
                  duration: 3000
                })
              }
            });            
          },
          fail: e => {
            console.error('[上传文件] 失败:', e)
          },
          complete: () => {
            wx.hideLoading()
          }
        })

      },
      fail: e => {
        console.error(e)
      }
    })
  },
  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值