使用FileReader对象读取文件内容,最后将文件内容进行处理使用
<template>
<a-upload :accept="acceptType.toString()" :showUploadList="false" :before-upload="beforeUpload" :customRequest="customUpload">
<i class="iconfont icon-daoru icon-size icon-hover"></i>
</a-upload>
</template>
<script>
const acceptType = ['application/json'];
const dataFn: any = ref();
const beforeUpload = (file) => {
dataFn.value = () => {
return new Promise((resolve,reject) => {
console.log(file);
if (acceptType.find((type) => type === file.type)) {
resolve(true);
} else {
message.warn('请上传正确格式文件');
reject(false);
}
}).finally(() => {});
};
};
// 自定义上传
const customUpload = (data) => {
dataFn
.value()
.then((res) => {
console.log(data,res);
if (res) {
const reader = new FileReader();
console.log(reader);
reader.onload = (e) => {
const fileContent = e.target.result;
console.log(fileContent,JSON.parse(fileContent));
};
reader.readAsText(data.file);
}
})
.catch((_err) => {});
};
</script>
reader.readAsText(data.file)中data.file的数据格式为
原文地址:https://blog.csdn.net/weixin_61977924/article/details/134176526
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。