From 9b70406e1349a2520036d12249aa759219d40af5 Mon Sep 17 00:00:00 2001 From: p34h2feab <2067143228@qq.com> Date: Sun, 13 Oct 2024 00:22:03 +0800 Subject: [PATCH] ADD file via upload --- index.js | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..0e07647 --- /dev/null +++ b/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