jQuery 使用javascript将数据写入本地文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16055391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:17:34  来源:igfitidea点击:

Writing data to a local text file with javascript

javascriptjqueryhtmlfile-iohta

提问by Madhusudhan Dollu

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

我已经创建了一个程序来将内容写入本地机器中的文本文件。

<form id="addnew">
    <input type="text" class="id">
    <input type="text" class="content">
    <input type="submit" value="Add">
</form>
<script>
    jQuery(function($) {
        $('#form_addjts').submit(function(){
            writeToFile({
                id: $(this).find('.id').val(), 
                content: $(this).find('.content').val()
            });
            return false;
        }); 
        function writeToFile(data){
            var fso = new ActiveXObject("Scripting.FileSystemObject");
            var fh = fso.OpenTextFile("D:\data.txt", 8);
            fh.WriteLine(data.id + ',' + data.content);
            fh.Close(); 
        } 
    }); 
</script>

This is working fine, and able to append my new data to the file.

这工作正常,并且能够将我的新数据附加到文件中。

But I want to update a particular row CONTENT based on the ID which I am passing.
I searched a lot, but could not find any.

但我想根据我传递的 ID 更新特定行 CONTENT。
我搜索了很多,但找不到任何。

How can update a particular row in the file based on the ID?

如何根据 ID 更新文件中的特定行?

Note:- I am not using any server as such. I have a a html file (contains all the functionality) which I will be running on my local machine itself.

注意:- 我没有使用任何服务器。我有一个 html 文件(包含所有功能),我将在我的本地机器上运行它。

采纳答案by akinuri

Our HTML:

我们的 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):

JS(写入txt文件):

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 .htafile and run it. Tested on W7, IE11. It's working. Also if you want me to explain what's going on, say so.

将这些放在一个.hta文件中并运行它。在 W7、IE11 上测试。它正在工作。另外,如果您想让我解释发生了什么,请说出来。