ADD file via upload

css
pg75sc2al 1 month ago
parent a23f103f81
commit 7cdeeeda3d

@ -0,0 +1,54 @@
const fileInput = document.getElementById('fileInput');
fileInput.addEventListener('change', (event) => {
const file = event.target.files[0];
if (!file) return;
// 读取文件并处理内容
const reader = new FileReader();
reader.onload = (e) => {
const data = new Uint8Array(e.target.result);
const workbook = XLSX.read(data, { type: 'array' });
const firstSheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[firstSheetName];
const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 });
names.length = 0; // 清空当前 names 数组
jsonData.forEach(row => {
if (row[0]) {
names.push(row[0]);
}
});
console.log(names); // 查看导入的名字
};
reader.readAsArrayBuffer(file);
// 显示文件名和按钮
handleFileSelect(event);
});
function handleFileSelect(event) {
const fileName = event.target.files[0].name; // 获取文件名
document.getElementById('output').textContent = `选择的文件: ${fileName}`; // 更新输出文本
// 显示进入点名按钮
document.getElementById('enterButton').style.display = 'block';
// 上传文件到后台
uploadFile(event.target.files[0]);
}
function uploadFile(file) {
const formData = new FormData();
formData.append('file', file); // 将文件添加到 FormData 对象
fetch('/upload', { // 假设后台路由是 /upload
method: 'POST',
body: formData,
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
})
.catch((error) => {
console.error('Error:', error);
});
}
Loading…
Cancel
Save