微信小程序云函数上传数据和图片 - 最佳实践
///'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 // 获取当前日期//n var date = new Date();//n var year = date.getFullYear();//n var month = date.getMonth() + 1;//n var day = date.getDate();//n//n // 调用云函数上传数据//n wx.cloud.callFunction({//n name: ///'addData///',//n data: {//n mailNumber: mailNumber,//n images: images,//n describe: describe,//n date: date//n },//n success: function(res) {//n console.log(///'云函数调用成功: ///', res);//n wx.showToast({//n title: res.result.message//n });//n },//n fail: function(err) {//n console.error(///'云函数调用失败: ///', err);//n wx.showToast({//n title: ///'上传失败///',//n });//n },//n complete: function() {//n wx.hideLoading();//n }//n });//n},//n//n// 云函数入口文件//nconst cloud = require(///'wx-server-sdk///')//n//ncloud.init()//n//n// 云函数入口函数//nexports.main = async (event, context) => {//n const wxContext = cloud.getWXContext()//n//n const mailNumber = event.mailNumber;//n const images = event.images;//n const describe = event.describe;//n const date = new Date();//n//n // Array to store the uploaded file IDs//n var uploadedFileIDs = [];//n//n // 加载图片到云存储//n for (var i = 0; i < images.length; i++) {//n const res = await cloud.uploadFile({//n cloudPath: ///'myImage////' + date.getFullYear() + ///'-///' + (date.getMonth() + 1) + ///'-///' + date.getDate() + ///'////' + new Date().getTime() + ///'_///' + Math.floor(Math.random() * 1000) + i + ///'.jpg/// ',//n filePath: images[i]//n });//n console.log(///'Upload successful: ///', res.fileID);//n uploadedFileIDs.push(res.fileID);//n }//n//n try {//n // 上传数据到云集合//n const db = cloud.database();//n const collection = db.collection(///'yddata///');//n const res = await collection.add({//n data: {//n mailNumber: mailNumber,//n images: uploadedFileIDs,//n describe: describe,//n date: date//n }//n });//n console.log(///'Data uploaded successfully: ///', res);//n return {//n success: true,//n message: ///'上传成功///'//n };//n } catch (err) {//n console.error(///'Data upload failed: ///', err);//n return {//n success: false,//n message: ///'上传失败///'//n };//n }//n
原文地址: http://www.cveoy.top/t/topic/pJM8 著作权归作者所有。请勿转载和采集!