这里用的是element-plus的标签,使用input type="file" 也是一样的
<script setup lang="ts">
import { ElMessage } from 'element-plus';
import { ref } from 'vue';
const txtContent = ref<string>('');
const onGetText = async (event: any) => {
const isExcel = event.name.endsWith(".json") || event.name.endsWith(".txt");
if (!isExcel) {
ElMessage.error('Please select json or txt !');
return;
}
const reader = new FileReader();
reader.onload = (e:any) => {
const content = e.target.result;
txtContent.value = content;
};
reader.readAsText(event.raw);
};
</script>
<template>
<p>{{ txtContent }}</p>
<el-upload action="#" :on-change="onGetText" accept=".json,.txt" class="upload-demo" name="formfile"
:auto-upload="false" :show-file-list="false">
<el-button type="primary"> 选择文本文件 </el-button>
</el-upload>
</template>