微信小程序云开发(笔记)————图片添加到指定文件夹

用小程序文档里边的方法上传图片,默认存在云储存的根目录,想要每个版块储存在不同文件夹内,可以直接在添加 【cloudPath】时,定义的文件名前面加上文件夹的名字

例如:const cloudPath = '文件夹名/' + 文件名

  ChooseImage: function() {
    var that = this
    const imgNum = 4 - that.data.swiperList.length
    // 选择图片
    wx.chooseImage({
      count: imgNum,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        wx.showLoading({
          title: '上传中',
        })
        const filePath = res.tempFilePaths[0]
        // 上传图片
        var timestamp = Date.parse(new Date());
        const cloudPath = 'set_up_img/' + timestamp + filePath.match(/\.[^.]+?$/)[0]
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: resa => {
              that.setData({
                swiperList: that.data.swiperList.concat(resa.fileID)
              })
          },
          fail: e => {
            wx.showToast({
              icon: 'none',
              title: '上传失败',
            })
          },
          complete: () => {
            wx.hideLoading()
          }
        })
      },
      fail: e => {
        console.error(e)
      }
    })
  },

猜你喜欢

转载自www.cnblogs.com/yeshengshudaixiong/p/11645694.html