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

<!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

评论

  1. 60.179.170.*2021-11-12 22:04:20
    不错,感谢分享

  2. 223.64.29.*2021-10-25 17:03:32
    谢谢