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