微信小程序雲開發(筆記)————圖片添加到指定文件夾


用小程序文檔里邊的方法上傳圖片,默認存在雲儲存的根目錄,想要每個版塊儲存在不同文件夾內,可以直接在添加 【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)
      }
    })
  },

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM