微信小程序云存储上传图片并保存数据到云数据库
saveAndUpload: function() {\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 // Create the file name using the current date\n var fileName = 'myImage/' + year + "-" + month + "-" + day + '/' + new Date().getTime() + "_" + Math.floor(Math.random() * 1000) + ".jpg"\n\n // Upload images to cloud storage\n for (var i = 0; i < images.length; i++) {\n wx.cloud.uploadFile({\n cloudPath: fileName,\n filePath: images[i],\n success: function(res) {\n console.log('Upload successful: ', res.fileID);\n wx.showToast({\n title: '上传成功',\n })\n },\n fail: function(err) {\n console.error('Upload failed: ', err);\n }\n });\n }\n\n // Upload data to cloud database collection\n const db = wx.cloud.database();\n const collection = db.collection('yddata');\n collection.add({\n data: {\n mailNumber: mailNumber,\n images: images,\n describe: describe,\n date: date\n },\n success: function(res) {\n console.log('Data uploaded successfully: ', res);\n },\n fail: function(err) {\n console.error('Data upload failed: ', err);\n }\n });\n}.bind(this)
原文地址: https://www.cveoy.top/t/topic/pwMi 著作权归作者所有。请勿转载和采集!