简体   繁体   English

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

[英]How to add data to file in javascript?

I want to make registration form and write the data into data.txt using ActiveXObject. 我想注册表格,然后使用ActiveXObject将数据写入data.txt。 What I write is 我写的是

<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? 谁能帮助我如何添加数据,而不是覆盖数据?

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 CreateTextFile so 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();
}

CreateTextFile overrwrites the current file, I think. 我认为CreateTextFile会覆盖当前文件。 You should use FileExists to check its presence before creating it. 在创建文件之前,应使用FileExists检查其存在。 If it does exist you can use OpenTextFile. 如果确实存在,则可以使用OpenTextFile。

Here's the relevant documentation 这是相关的文档

Disclaimer You should never use those functions. 免责声明切勿使用这些功能。 They only work in IE and are horrible. 他们只能在IE中工作,而且太可怕了。

I think your problem stems from using CreateTextFile . 我认为您的问题源于使用CreateTextFile You should instead be using OpenTextFile with the second parameter set to 8 . 您应该使用第二个参数设置为8 OpenTextFile That will allow for appending. 这将允许追加。

Use OpenTextFile method with create flag and ForAppending mode instead of CreateTextFile . OpenTextFile方法与create标志和ForAppending模式一起使用,而不要使用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. 但是,请一定要理解,您不仅将自己限制在非常老的IE版本和受信任区域内,而且还将文件保留在用户的本地驱动器上,而不是服务器上。 Because of that you can't do anything with this "registration data". 因此,您无法使用此“注册数据”做任何事情。

use ("E:\\\\Test.txt", 8); 使用("E:\\\\Test.txt", 8); . 8 is a mode to append. 8是附加模式。 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 AvtiveXObject line 因此,我想知道您是否拥有与我相同的作业,因为我也遇到相同的问题,但是我陷入了AvtiveXObject

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM