Writing data to a local text file with javascript

前端 未结 1 360
-上瘾入骨i
-上瘾入骨i 2020-12-01 06:50

I have created a procedure to write content to a text file in my local machine.

&
相关标签:
1条回答
  • 2020-12-01 07:50

    Our HTML:

    <div id="addnew">
        <input type="text" id="id">
        <input type="text" id="content">
        <input type="button" value="Add" id="submit">
    </div>
    
    <div id="check">
        <input type="text" id="input">
        <input type="button" value="Search" id="search">
    </div>
    

    JS (writing to the txt file):

    function writeToFile(d1, d2){
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        var fh = fso.OpenTextFile("data.txt", 8, false, 0);
        fh.WriteLine(d1 + ',' + d2);
        fh.Close();
    }
    var submit = document.getElementById("submit");
    submit.onclick = function () {
        var id      = document.getElementById("id").value;
        var content = document.getElementById("content").value;
        writeToFile(id, content);
    }
    

    checking a particular row:

    function readFile(){
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        var fh = fso.OpenTextFile("data.txt", 1, false, 0);
        var lines = "";
        while (!fh.AtEndOfStream) {
            lines += fh.ReadLine() + "\r";
        }
        fh.Close();
        return lines;
    }
    var search = document.getElementById("search");
    search.onclick = function () {
        var input   = document.getElementById("input").value;
        if (input != "") {
            var text    = readFile();
            var lines   = text.split("\r");
            lines.pop();
            var result;
            for (var i = 0; i < lines.length; i++) {
                if (lines[i].match(new RegExp(input))) {
                    result = "Found: " + lines[i].split(",")[1];
                }
            }
            if (result) { alert(result); }
            else { alert(input + " not found!"); }
        }
    }
    

    Put these inside a .hta file and run it. Tested on W7, IE11. It's working. Also if you want me to explain what's going on, say so.

    0 讨论(0)
提交回复
热议问题