admin管理员组

文章数量:1327262

I want to make registration form and write the data into data.txt using ActiveXObject. 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.

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

I want to make registration form and write the data into data.txt using ActiveXObject. 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.

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

Share Improve this question edited Dec 24, 2015 at 21:10 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 17, 2012 at 16:06 greenthundergreenthunder 7279 gold badges19 silver badges34 bronze badges 10
  • 1 @greenthunder can you tell me what college is that so I can stop anyone I know from attending ever? – Alex Turpin Commented Apr 17, 2012 at 16:10
  • 1 @greenthunder I understand, but as a college teacher myself, I don't think anyone should ever teach VBScript ever. – Alex Turpin Commented Apr 17, 2012 at 16:16
  • 1 @Xeon06 So all classic ASP is irrelevant and all legacy systems should be disregarded? – Snuffleupagus Commented Apr 17, 2012 at 16:17
  • 1 @user1090190 Yes, haha. But let me rephrase. I don't think anyone should ever teach client-side VBScript. Ever. – Alex Turpin Commented Apr 17, 2012 at 16:18
  • 1 @user1090190 yes, that is true. But I don't think it gives students the right kind of experience and concepts, especially those starting out in web. At the very least, teach them JavaScript and PHP / ASP.NET first, and when they have a good understanding of it, you can show them VBScript. The fact that it is both used client-side and server-side can be very confusing for begginers. – Alex Turpin Commented Apr 17, 2012 at 16:28
 |  Show 5 more ments

5 Answers 5

Reset to default 1

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

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

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:

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. You should use FileExists to check its presence before creating it. If it does exist you can use OpenTextFile.

Here's the relevant documentation

Use OpenTextFile method with create flag and ForAppending mode instead of 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".

you can use this npm package for that one.

https://www.npmjs./package/docfillx
npm i docfillx

本文标签: overridingHow to add data to file in javascriptStack Overflow