在vue3中读取本地txt文件

发布时间 2023-03-29 11:40:48作者: 月下云生

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

上传文件:

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

 操作函数:

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)
}

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