saveAndUpload: function() {\n wx.showLoading({\n title:'数据上传中...',\n mask:true\n })\n // 保存邮件编号和图片到数据库\n const mailNumber = this.data.mailNumber;\n const images = this.data.images;\n const describe = this.data.describe;\n // Get the current date\n var date = new Date();\n var year = date.getFullYear();\n var month = date.getMonth() + 1;\n var day = date.getDate();\n\n // Array to store the uploaded file URLs\n var uploadedFileURLs = [];\n\n // 加载图片到云存储\n for (var i = 0; i < images.length; i++) {\n wx.cloud.uploadFile({\n cloudPath: 'myImage/' + year + "-" + month + "-" + day + '/' + new Date().getTime() + "_" + Math.floor(Math.random() * 1000) + i + ".jpg",\n filePath: images[i],\n success: function(res) {\n console.log('Upload successful: ', res.fileID);\n uploadedFileURLs.push(res.fileID);\n if (uploadedFileURLs.length === images.length) {\n // 调用云函数上传数据\n wx.cloud.callFunction({\n name: 'addData',\n data: {\n mailNumber: mailNumber,\n images: uploadedFileURLs,\n describe: describe,\n date: date\n },\n success: function(res) {\n console.log('Data uploaded successfully: ', res);\n wx.showToast({\n title: '上传成功',\n })\n },\n fail: function(err) {\n console.error('Data upload failed: ', err);\n wx.showToast({\n title: '上传失败',\n })\n }\n }).then(res=>{\n console.log(res)\n wx.hideLoading({\n complete:(res)=>{},\n })\n });\n }\n },\n fail: function(err) {\n console.error('Upload failed: ', err);\n }\n });\n }\n}

微信小程序云函数上传数据 - 使用云函数addData优化上传流程

原文地址: http://www.cveoy.top/t/topic/pJMZ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录