vue识别txt文件

时间:2025-03-28 11:26:33
  • <template>
  • <div>
  • <el-button @click="txtBtn">扫描txt文件</el-button>
  • <input
  • type="file"
  • @change="inp"
  • id="txt"
  • style="display: none"
  • />
  • <div>{{ gg }}</div>
  • </div>
  • </template>
  • <script>
  • export default {
  • data() {
  • return {
  • gg: "aaaa",
  • };
  • },
  • methods: {
  • // txt文本
  • txtBtn() {
  • ("txt").click();
  • },
  • inp(e) {
  • // 从事件对象中获取用户选择的文件
  • const file = [0];
  • let name = file.(".").splice(-1).toString();
  • if (name !== "txt") {
  • this.$("文件类型错误,请重新选择文件");
  • return;
  • }
  • // 创建一个 FileReader 对象后,你可以使用该对象的方法来读取文件内容。
  • const reader = new FileReader();
  • if (typeof FileReader === "undefined") {
  • alert("您的浏览器不支持FileReader接口");
  • }
  • // 用于处理文件读取成功
  • = (e) => this.$emit("load", ());
  • // 将文件内容作为文本读取
  • (file, "utf-8");
  • },
  • dealNum(item) {
  • = item;
  • (item);
  • },
  • },
  • };
  • </script>
  • <style></style>