<template> <div> <el-button @click="txtbtn">插入文本</el-button> <input type="file" @change="loadTextFromFile" id="txt" style="display:none" /> <div>{{gg}}</div> </div> </template> <script> export default { data() { return { gg: "111111", }; }, methods: { // txt文本 txtbtn() { document.getElementById("txt").click(); }, loadTextFromFile(e) { const file = e.target.files[0]; let name = file.name.split(".").splice(-1).toString(); if (name !== "txt") { this.$message.success("文件类型错误,请重新选择文件"); return; } const reader = new FileReader(); if (typeof FileReader === "undefined") { alert("您的浏览器不支持FileReader接口"); } reader.onload = (e) => this.$emit("load", this.dealNum(e.target.result)); reader.readAsText(file, "utf-8"); }, dealNum(item) { this.gg = item; console.log(item); }, }, }; </script> <style> </style>
标签:vue,name,FileReader,gg,file,reader,txt,加载 From: https://www.cnblogs.com/cc0518/p/16834995.html