From 55db1c15c21cab42369738731cb8e2776d7a9796 Mon Sep 17 00:00:00 2001 From: pg78ac9nr <2401677463@qq.com> Date: Sat, 12 Oct 2024 16:11:20 +0800 Subject: [PATCH] ADD file via upload --- 3.0 pages index.js | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 3.0 pages index.js diff --git a/3.0 pages index.js b/3.0 pages index.js new file mode 100644 index 0000000..0e07647 --- /dev/null +++ b/3.0 pages index.js @@ -0,0 +1,76 @@ +Page({ + data: { + fileID: null // 初始化为 null + }, + + // 选择文件 + chooseFile: function() { + wx.chooseMessageFile({ + count: 1, + type: 'file', + success: (res) => { + console.log('Selected file:', res.tempFiles[0]); + const tempFilePath = res.tempFiles[0].path; // 获取临时文件路径 + + // 上传文件到云存储 + wx.cloud.uploadFile({ + cloudPath: `files/${res.tempFiles[0].name}`, // 云存储路径 + filePath: tempFilePath, // 临时文件路径 + success: (uploadRes) => { + console.log('File uploaded successfully:', uploadRes); + const fileID = uploadRes.fileID; // 获取上传后的文件ID + console.log('fileID:', fileID); // 确认 fileID 的值 + this.setData({ + fileID: fileID + }); + }, + fail: (err) => { + console.error('Failed to upload file:', err); + } + }); + }, + fail: (err) => { + console.error('Failed to choose file:', err); + } + }); + }, + + // 导入名单 + importStudents: function() { + const fileID = this.data.fileID; + + if (!fileID) { + wx.showToast({ + title: '请选择文件', + icon: 'none' + }); + return; + } + + wx.cloud.callFunction({ + name: 'importstudents', + data: { + fileID: fileID + }, + success: (res) => { + console.log('Import students success:', res.result); + wx.showToast({ + title: '名单导入成功', + icon: 'success' + }); + + // 跳转到点名页面 + wx.navigateTo({ + url: '/pages/rollcall/rollcall' + }); + }, + fail: (err) => { + console.error('Import students failed:', err); + wx.showToast({ + title: '名单导入失败', + icon: 'none' + }); + } + }); + } +}); \ No newline at end of file