ADD file via upload

main
p34h2feab 1 month ago
parent 8216ce1aad
commit 9b70406e13

@ -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'
});
}
});
}
});
Loading…
Cancel
Save