前端JavaScript读取上传文件信息

释放双眼,带上耳机,听听看~!
<template>
    <div>
        <el-upload
            class="upload-demo"
            drag
            action=""
            :show-file-list="false"
            :http-request="uploadFile"
            multiple
        >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">
                将文件拖到此处,或<em>点击上传</em>
                <p>注意一行不能超过1024字节</p>
            </div>
        </el-upload>
    </div>
</template>

<script>
export default {
    data() {
        return {
            input: "",
            file: null,
        };
    },
    methods: {
        uploadFile({ file }) {
            this.file = file;

            if (this.file.type != "text/plain") {
                this.$message.error("仅支持上传txt");
                return;
            }
            let reader = new FileReader();
            console.log("reader", reader);
            reader.onload = (evt) => {
                if (evt.target.readyState !== 2) return;
                if (evt.target.error) {
                    console.log("Error while reading file");
                    return;
                }

                const chunkContent = evt.target.result || "";
                let reg = /(rn?|n)/gm;
                let result;
                if ((result = reg.exec(chunkContent)) !== null) {
                    //读取一行
                    // let cursor = result.index;
                    // this.input = chunkContent.substr(0, cursor);
                    // 读取指定行数
                    console.log("chunkContent", chunkContent.split(/s+/, 10));
                    this.input = chunkContent.split(/s+/, 10).map(Number);
                    console.log("input", this.input);
                } else {
                    this.input = chunkContent;
                    console.log("chunkContent".chunkContent);
                }

                this.file = null;
            };

            reader.readAsText(this.file.slice(0, 1024));
        },
    },
};
</script>

<style>
</style>
内容投诉
Vue

Element-DateTimePicker 日期时间选择器 禁用已过期的日期和时间

2021-8-23 13:42:50

JavaScript

Js中伪数组冒泡排序的两种方法

2020-7-27 18:01:43

2 条回复 A文章作者 M管理员
  1. 人间烟火

    111

  2. 人间烟火

    111

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索