前端验证上传的文件为json
const fileInput = document.getElementById('fileInput');
fileInput.addEventListener('change', (event) => {
const file = event.target.files[0];
if (file.type === 'application/json') {
console.log('这是一个JSON文件');
} else {
alert('请上传一个JSON文件');
fileInput.value = '';
}
});
拿到上传文件的具体内容
const fileInput = document.getElementById('fileInput');
fileInput.addEventListener('change', (event) => {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = (e) => {
const content = e.target.result;
console.log('文件内容:', content);
const jsonData = JSON.parse(content);
console.log('解析后的JSON:', jsonData);
};
reader.readAsText(file);
});