This repository has been archived on 2024-09-30. You can view files and clone it, but cannot push or open issues/pull-requests.
SmartRollCall/app/static/js/upload_excel.js

45 lines
1.7 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// 获取上传按钮和文件输入元素
var uploadBtn = document.getElementById('uploadExcel');
var fileInput = document.getElementById('excelFile');
// 当点击上传按钮时触发文件输入的点击事件
uploadBtn.addEventListener('click', function () {
fileInput.click();
});
// 处理文件选择事件
fileInput.addEventListener('change', function () {
var file = this.files[0]; // 获取文件对象
if (file) {
// 检查文件类型
var fileName = file.name;
var fileExt = fileName.split('.').pop().toLowerCase();
if (fileExt === 'xlsx' || fileExt === 'xls') {
// 使用 FormData 上传文件
var formData = new FormData();
formData.append('file', file, fileName); // 'file' 是你的服务器端期待的字段名
// 使用 fetch 发送文件
fetch('/api/receive-excel', {
method: 'POST',
body: formData // 传递表单数据
})
.then(response => {
if (response.ok) {
return response.json(); // 如果上传成功解析JSON响应
}
throw new Error('Network response was not ok.'); // 如果上传失败,抛出错误
})
.then(data => {
// 处理响应数据
layer.msg('上传成功!'); // 弹出成功消息
})
.catch(error => {
console.error('Upload failed:', error);
layer.msg("文件上传失败!"); // 弹出失败消息
});
} else {
alert("请上传Excel文件!");
}
}
});