微信小程序之上传多张图片(云开发)

一、简介:

这篇文章向大家展示的是把图片上传到云数据库中,这是我做商城项目时研究的。大家都知道,云开发是没有后端开发的,所有图片我们要放到云数据库中。

二、素材图:

在这里插入图片描述
在这里插入图片描述

三、效果图:

在这里插入图片描述
在这里插入图片描述

四、代码:

wxml:

<!--miniprogram/pages/fb/fb.wxml-->
<view class='pages'>
  <view class='top'><text class='top_name'>商品图片:</text></view>
  <!-- 图片 -->
  <view class="images_box">
    <block wx:key="imgbox" wx:for="{{imgbox}}">
      <view class='img-box'>
        <image class='img' src='{{item}}'></image>
        <view class='img-delect' data-deindex='{{index}}' bindtap='imgDelete1'>
          <image class='img' src='../../images/delect.png'></image>   
        </view>
      </view>
    </block>
    <view class='img-box' bindtap='addPic1' wx:if="{{imgbox.length<9}}">
      <image class='img' src='../../images/add_image.png'></image>   
    </view>
  </view>
  <button bindtap='fb'>上传图片</button>
</view>

wxss:

/* miniprogram/pages/fb/fb.wxss */
page{
  background-color: rgba(200, 198, 201, 0.527);
}
.pages{
  width: 98%;
  margin: auto;
  overflow: hidden;
}
.top{
  width: 100%;
  overflow: hidden;
  margin: auto;
  font-size: 50rpx;
  background-color: white;
  border-left: 8rpx solid rgb(9, 245, 60);
  border-bottom: 1rpx solid rgba(117, 116, 116, 0.527);
}
.top_name{
  margin-left: 20rpx;
}
/* 图片 */
.images_box{
  width: 100%;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: flex-start;
  background-color: white;
}
.img-box{
  border: 5rpx;
  border-style: solid;
  border-color: rgba(0, 0, 0, 0.452);
  width: 200rpx;
  height: 200rpx;
  margin-left: 35rpx;
  margin-top: 20rpx;
  margin-bottom: 20rpx;
  position: relative;
}
/* 删除图片 */
.img-delect{
  width:50rpx;
  height:50rpx;
  border-radius:50%;
  position:absolute;
  right:-20rpx;
  top:-20rpx;
}
.img{
  width: 100%;
  height: 100%;
}
.jiage{
  height: 60rpx;
  width: 90%;
  margin-left: 5%;
  margin-right: 5%;
  background-color: white;
  display: flex;
  justify-content: flex-start;
}
.rmb{
  width: 280rpx;
  border: 2rpx solid rgb(199, 197, 197);
}
button{
  margin-top: 20rpx;
  background-color: green;
}
.radio-group{
  display: flex;
}

js:

// pages/fb/fb.js
const app = getApp()
const db = wx.cloud.database();//初始化数据库
Page({
  /**
    * 页面的初始数据
    */
  data: {
    imgbox: [],//选择图片
    fileIDs: [],//上传云存储后的返回值
  },

  // 删除照片 &&
  imgDelete1: function (e) {
    let that = this;
    let index = e.currentTarget.dataset.deindex;
    let imgbox = this.data.imgbox;
    imgbox.splice(index, 1)
    that.setData({
      imgbox: imgbox
    });
  },
  // 选择图片 &&&
  addPic1: function (e) {
    var imgbox = this.data.imgbox;
    console.log(imgbox)
    var that = this;
    var n = 5;
    if (5 > imgbox.length > 0) {
      n = 5 - imgbox.length;
    } else if (imgbox.length == 5) {
      n = 1;
    }
    wx.chooseImage({
      count: n, // 默认9,设置图片张数
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
      success: function (res) {
        // console.log(res.tempFilePaths)
        // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths

        if (imgbox.length == 0) {
          imgbox = tempFilePaths
        } else if (5 > imgbox.length) {
          imgbox = imgbox.concat(tempFilePaths);
        }
        that.setData({
          imgbox: imgbox
        });
      }
    })
  },

  //图片
  imgbox: function (e) {
    this.setData({
      imgbox: e.detail.value
    })
  },


  //发布按钮
  fb: function (e) {
    if (!this.data.imgbox.length) {
      wx.showToast({
        icon: 'none',
        title: '图片类容为空'
      });
    } else {
        //上传图片到云存储
        wx.showLoading({
          title: '上传中',
        })
        let promiseArr = [];
        for (let i = 0; i < this.data.imgbox.length; i++) {
          promiseArr.push(new Promise((reslove, reject) => {
            let item = this.data.imgbox[i];
            let suffix = /\.\w+$/.exec(item)[0];//正则表达式返回文件的扩展名
            wx.cloud.uploadFile({
              cloudPath: new Date().getTime() + suffix, // 上传至云端的路径
              filePath: item, // 小程序临时文件路径
              success: res => {
                this.setData({
                  fileIDs: this.data.fileIDs.concat(res.fileID)
                });
                console.log(res.fileID)//输出上传后图片的返回地址
                reslove();
                wx.hideLoading();
                wx.showToast({
                  title: "上传成功",
                })
              },
              fail: res=>{
                wx.hideLoading();
                wx.showToast({
                  title: "上传失败",
                })
              }

            })
          }));
        }
        Promise.all(promiseArr).then(res => {//等数组都做完后做then方法
          console.log("图片上传完成后再执行")
          this.setData({
            imgbox:[]
          })
        })

      }
  },

})
  • 37
    点赞
  • 144
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 18
    评论
微信小程序开发中,可以使用阿里云的一些服务来支持后端功能。首先,如果之前没有免费试用过阿里云的服务器,你可以通过访问https://free.aliyun.com 来免费试用阿里云的服务器资源。 对于前后端分离的微信小程序开发,后端代码可以直接放到ROOT文件夹下,前端可以通过接口调用后端代码。这种架构可以实现前后端的分离和交互。 在这个开发实战中,我们还涉及了阿里云的一些其他云资源的使用。其中包括物联网平台IoT,它提供了设备接入、数据流转和指令下行的能力;还有函数计算FC,它是一种Serverless计算平台,可以用于处理一些计算任务。 综上所述,通过阿里云的一些服务,我们可以支持微信小程序开发的后端功能,并且可以利用物联网平台和函数计算等云资源来实现更多的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [开发微信小程序之阿里云服务器搭建|前后端分离](https://blog.csdn.net/weixin_47129439/article/details/118818489)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [智能家居:微信小程序与阿里云IOT设备交互实战](https://download.csdn.net/download/weixin_38704786/14013977)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Memory沙漏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值