JavaScript读取本地Json或txt文本文件

首次发布:2021-05-16 11:34
<!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>上传文件</title>
</head>
<body>
    <input type="file" id="testJsonFile" onchange="openFile(event)" >

    <script type="text/javascript">
        //获取到选中的文件
        var openFile = function (event) {
            var input = event.target;
            var reader = new FileReader();
            reader.onload = function () {
                if (reader.result) {
                    //显示文件内容
                    console.log(reader.result);
                }
            };
            reader.readAsText(input.files[0]);
        };
    </script>
</body>
</html>
视频讲解地址 https://www.bilibili.com/video/BV1LL4y167re