微信小程序云函数上传数据 - 使用云函数addData
saveAndUpload: function () { wx.showLoading({ title: '数据上传中...', mask: true }) // 保存邮件编号和图片到数据库 const mailNumber = this.data.mailNumber; const images = this.data.images; const describe = this.data.describe; // Get the current date var date = new Date(); var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate();
// Array to store the uploaded file IDs
var uploadedFileIDs = [];
// 加载图片到云存储
for (var i = 0; i < images.length; i++) {
wx.cloud.uploadFile({
cloudPath: 'myImage/' + year + '-' + month + '-' + day + '/' + new Date().getTime() + '_' + Math.floor(Math.random() * 1000) + i + '.jpg',
filePath: images[i],
success: function (res) {
console.log('Upload successful: ', res.fileID);
uploadedFileIDs.push(res.fileID);
if (uploadedFileIDs.length === images.length) {
// 调用云函数addData
wx.cloud.callFunction({
name: 'addData',
data: {
mailNumber: mailNumber,
images: uploadedFileIDs,
describe: describe,
date: date
},
success: function (res) {
console.log('Data uploaded successfully: ', res);
wx.showToast({
title: '上传成功',
})
},
fail: function (err) {
console.error('Data upload failed: ', err);
wx.showToast({
title: '上传失败',
})
}
}).then(res => {
console.log(res)
wx.hideLoading({
complete: (res) => { },
})
});
}
},
fail: function (err) {
console.error('Upload failed: ', err);
}
});
}
}
原文地址: http://www.cveoy.top/t/topic/pJNm 著作权归作者所有。请勿转载和采集!