javascript 如何在javascript中将数据添加到文件中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10194871/
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-10-26 08:59:22  来源:igfitidea点击:

How to add data to file in javascript?

javascriptoverridingusingactivexobject

提问by greenthunder

I want to make registration form and write the data into data.txt using ActiveXObject. What I write is

我想使用 ActiveXObject 制作注册表并将数据写入 data.txt。我写的是

<script type="text/javascript">
    function WriteFile()
    {
       var fso  = new ActiveXObject("Scripting.FileSystemObject");
       var fh = fso.CreateTextFile("E:\Test.txt", true);
       x=document.getElementById("name").value;
       y=document.getElementById("password").value;
       fh.WriteLine(x+"#"+y);
       fh.Close();
    }
</script>
<BODY>
<form>
    <input type="text" id="name"/>
    <input type="text" id="password"/>
    <input type="button" value="Sign Up" id="write" onclick="WriteFile()"/>
</form>
</BODY>

When I tried that way, every time I click Sign Up button, the new data override the previous data. I tried to use fh.AppendLine(x + "#" + y)but it didn't work.

当我尝试这种方式时,每次单击“注册”按钮时,新数据都会覆盖以前的数据。我尝试使用,fh.AppendLine(x + "#" + y)但没有用。

Could anyone help me how to add data, not override data?

谁能帮助我如何添加数据,而不是覆盖数据?

回答by noob

I've done stuff like this a long time ago... (when I was using windows) I think it is because you're replacing the file with a new file with CreateTextFileso if the file already exists you'll need to do this:

很久以前我做过这样的事情......(当我使用Windows时)我认为这是因为你正在用一个新文件替换文件CreateTextFile所以如果文件已经存在你需要这样做:

function AppendLine()
{
   var fso  = new ActiveXObject("Scripting.FileSystemObject");
   var fh = fso.OpenTextFile("E:\Training Asslab\Advance\Write to File\Test.txt", 8, True);
   x=document.getElementById("name").value;
   y=document.getElementById("password").value;
   fh.WriteLine(x+"#"+y);
   fh.Close();
}

回答by Snuffleupagus

CreateTextFile overrwrites the current file, I think. You should use FileExists to check its presence before creating it. If it does exist you can use OpenTextFile.

我认为 CreateTextFile 会覆盖当前文件。在创建它之前,您应该使用 FileExists 检查它的存在。如果确实存在,您可以使用 OpenTextFile。

Here's the relevant documentation

这是相关文档

回答by Alex Turpin

DisclaimerYou should never use those functions. They only work in IE and are horrible.

免责声明您永远不应使用这些功能。它们只能在 IE 中工作,而且很糟糕。

I think your problem stems from using CreateTextFile. You should instead be using OpenTextFilewith the second parameter set to 8. That will allow for appending.

我认为您的问题源于使用CreateTextFile. 您应该OpenTextFile将第二个参数设置为8. 这将允许附加。

回答by Oleg V. Volkov

Use OpenTextFilemethod with create flag and ForAppending mode instead of CreateTextFile.

使用带有创建标志和 ForAppending 模式的OpenTextFile方法而不是CreateTextFile.

However, do understand, that you're not only limiting yourself to very old IE version and inside trusted zone, but you're also leaving files on user's local drives, not on your server. Because of that you can't do anything with this "registration data".

但是,请理解,您不仅将自己限制在非常旧的 IE 版本和受信任区域内,而且还将文件留在用户的本地驱动器上,而不是您的服务器上。因此,您无法使用此“注册数据”做任何事情。

回答by aquatorrent

use ("E:\\Test.txt", 8);. 8 is a mode to append. and thus, i'm wondering whether you're having the same homework as me or not, since i'm having the same problem too, but i'm stuck in the AvtiveXObjectline

使用("E:\\Test.txt", 8);. 8 是一种追加模式。因此,我想知道你是否有和我一样的家庭作业,因为我也有同样的问题,但我被困在了这AvtiveXObject条线上