Javascript:如何将数据追加到文件 [英] Javascript: how to append data to a file

查看:988
本文介绍了Javascript:如何将数据追加到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用javascript将数据附加到文件?

how can i append data to a file using javascript?

我尝试使用此代码,但出现错误:

i tried to use this code, but i got an error:

var fso = new ActiveXObject("Scripting.FileSystemOject");
var filepath = fso.GetFile("member.txt");
var fileObject = fso.OpenTextFile(filepath, 8);
file.WriteLine(id + "|" + pass);
fileObject.close();

错误出现在 var fso = new ActiveXObject( Scripting.FileSystemOject ); ,写为:错误:自动化服务器无法创建对象

是有没有其他方法可以使用javascript来附加文件或解决此问题?谢谢:)

is there any other way to append the file using javascript or the way to fix this? thanks :)

编辑:
i已经做了,但仍无法正常工作:/

i have doing what's written on this, and it still not working :/

推荐答案

我刚刚在您的代码中意识到了这些问题:

I just realized these in your code:

var fileObject = fso.OpenTextFile(filepath, 8,true);

您将需要 true 参数,如果文件不存在,或者您要覆盖/附加文件。

You'll need the true-argument, if the file does not exist, or you want to overwrite/append it.

var filepath = fso.GetFile("member.txt");// This won't work.
var filepath = "your_filePath"; // Use this instead
var fileObject = fso.OpenTextFile(filepath, 8, true);

OpenTextFile()需要一个路径像 D:/test/file.txt 这样的字符串。 GetFile()返回一个对象,您可以看到作为字符串( D:Dtest\file。 txt ),但这不是字符串。还使用绝对路径,相对路径似乎不适合我的经验。

OpenTextFile() needs a path as a string like "D:/test/file.txt". GetFile() returns an object, which you can see as a string (D:\test\file.txt), but it's not a string. Use also absolute paths, relative paths don't seem to work by my experience.

编辑

将以下代码添加到html文件的< head> -部分,然后在本地另存为hta(文件扩展名为 hta ,而不是 htm html )。

Add the code below to the <head>-part of your html-file, then save locally as a hta (with file extension hta, not htm or html).

<hta:application
  applicationName="MyApp"
  id="myapp"
  singleInstance="yes"
/>

然后运行hta文件。如果仍然出现ActiveX错误,则操作系统不支持该错误。如果这样做可行,则说明您尚未完成所有正确的安全设置。

Then run the hta-file. If you still getting an ActiveX-error, it's not supported by your OS. If this works, you haven't done all the security settings correct.

EDIT II

在这种情况下,通过ActiveX获取路径不是很有用,无论如何,您都需要将其写成文字。而且我不应该做家庭作业,但这可以解决问题……

In this case it's not very usefull to get the path through ActiveX, you'll need to write it literal anyway. And I'm not supposed to do your homeworks, but this does the trick...

var filepath = new String(fso.GetFile("member.txt")).replace(/\\/g,'/');

别忘了我上面所说的关于使用绝对路径的事情...

And don't forget what I've said above about using absolute paths...

这篇关于Javascript:如何将数据追加到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆