在vue3中读取本地txt文件

碰到运营提出需求,提供了一个.txt文件,要求输入框校验文件提供的敏感词汇,故以此记录:

上传文件:

1
<input @change="uploadFile" type="file">

 操作函数:

1
2
3
4
5
6
7
8
9
10
11
12
const fileContent = ref('');
 
const uploadFile = async(event: any) => {
    console.log(event)
    const file = event.target.files[0]
    const filePath = URL.createObjectURL(file)
    const response = await fetch(filePath)
    const filteredText: any =await response.text()
    // 文件内格式处理
    fileContent.value = filteredText.split('\n').join('').split('\r')
    console.log(fileContent.value)
}

  由于,文件内词汇数量过多,后改用后端提示验证

 

 

posted @   月下云生  阅读(1950)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
点击右上角即可分享
微信分享提示