카테고리 없음

js 파일 읽기

최무회 2020. 11. 5. 23:47
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Enter key Press Action</title>
    <script>
        function openTextFile() {
            var input = document.createElement("input");
            input.type = "file";
            input.accept = "text/plain"; // 확장자가 xxx, yyy 일때, ".xxx, .yyy"
            input.onchange = function(event) {
                processFile(event.target.files[0]);
            };
            input.click();
        }

        function processFile(file) {
            var reader = new FileReader();
            reader.onload = function() {
                output.innerText = reader.result;
            };
//            reader.readAsText(file, /* optional */ "euc-kr");
            reader.readAsText(file, /* optional */ "utf-8");
        }

    </script>
</head>

<body>
    <button onclick="openTextFile()">Open</button>
    <div id="output">...</div>

</body>

</html>