云开发上传多张图片,欢迎大佬指点
发布于 4 年前 作者 panna 690 次浏览 来自 分享

const db = wx.cloud.database(); // 初始化数据库

Page({

data: {

images: [], // 上传的图片

fileIds: [],

},

submit: function () {

// 上传图片到云存储

let promiseArr = [];

for (let i = 0; i < this.data.images.length; i++) {

promiseArr.push(new Promise((reslove, reject) => {

let item = this.data.images[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)

});

reslove();

},

fail: console.error

})

}));

}

Promise.all(promiseArr).then(res => {

// 插入数据

db.collection('top_op').add({

data: {

fileIds: this.data.fileIds

}

}).then(res => {}).catch(err => {})


});


},

uploadImg: function () {

// 选择图片

wx.chooseImage({

count: 9,

sizeType: ['original', 'compressed'],

sourceType: ['album', 'camera'],

success: res => {

// tempFilePath可以作为img标签的src属性显示图片

const tempFilePaths = res.tempFilePaths

console.log(tempFilePaths);

this.setData({

images: this.data.images.concat(tempFilePaths)

});

this.submit()

}

})

},

})

1 回复
回到顶部