使用 JavaScript 写入文本文件?

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

Use JavaScript to write to text file?

javascriptactivexactivexobject

提问by Robert

I have a need to capture the topics of a search from one of my blog pages and write it to text file for followup. I have been able to capture the input string, however, am not able to write it to a text file. I have read and tried to implement all the solutions I have found with no success. I include a very simple sample of code to write text to file but I can't get it to work. The Onblur event functions fine to access myFunction, so that's not problem. I'm going crazy as samples shown from others are so simple.

我需要从我的博客页面之一中捕获搜索主题并将其写入文本文件以进行后续操作。我已经能够捕获输入字符串,但是,无法将其写入文本文件。我已阅读并尝试实施我发现的所有解决方案,但均未成功。我包含了一个非常简单的代码示例来将文本写入文件,但我无法让它工作。Onblur 事件可以很好地访问 myFunction,所以这不是问题。我快疯了,因为其他人展示的样本太简单了。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" >
    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>fs=Server CreateObject ("Scripting FileSystemObject")</title>
    <script type="text/javascript">
    function myFunction()
    {
        var fs = ActiveXObject("Scripting.FileSystemObject");
        var f = fs.OpenTextFile("d:\test\topics.txt", 8, true);
        f.WriteLine("This text will be added to the end of file");
        f.Close();
    }
    </script>
    </head>

    <body>
    <input name="Text1" type="text" size="55" onblur="myFunction()" />

    </body>

    </html>

回答by Incognito

If your're using modern browsers which support data-uri, you can create content with javascript and force a download by base64 encoding it and making it compatible with data-uri, then simply pointing window location at it.

如果您使用支持 data-uri 的现代浏览器,您可以使用 javascript 创建内容并通过 base64 编码强制下载并使其与 data-uri 兼容,然后只需将窗口位置指向它。

This answer demonstrates an HTML-only way to do it.

此答案演示了一种仅使用 HTML 的方法。

You can also use a flash helper to take care of this. For instance, Downloadify does this.

您还可以使用 flash 助手来处理此问题。例如,Downloadify 就是这样做的。

回答by Bart Platak

JavaScript cannotread/write files from the client or write files to the server (directly).

JavaScript无法从客户端读取/写入文件或将文件写入服务器(直接)。

If you want to write files to the server consider using server-side scripts that will interact with your javascript - and exampleon how to do this with a little bit of PHP.

如果您想将文件写入服务器,请考虑使用将与您的 javascript 交互的服务器端脚本 - 以及有关如何使用一点 PHP 执行此操作的示例

In terms of client file interaction this is simply impossible for security reasons (unless it is uploaded to the script in which case it is read accessible).

在客户端文件交互方面,出于安全原因,这是不可能的(除非它被上传到脚本,在这种情况下它是可读取的)。

You can however store data on clients - I have previously been using jStorageand highly recommend it. It works cross-browser (even on IE 6) and is extremely easy to use.

但是,您可以在客户端上存储数据 - 我以前一直在使用jStorage并强烈推荐它。它可以跨浏览器工作(甚至在 IE 6 上)并且非常易于使用。

回答by Robert

Thanks for all the suggestions provided by the members, I was able to perform writing and appending to text file on server. I used html code to acquire my data needed for writing and then called a .php file to do the writing to file. It was a client side versus server side issue and the .php solved it, using fopen, fwrite and fclose.

感谢成员提供的所有建议,我能够在服务器上执行写入和附加到文本文件。我使用 html 代码来获取写入所需的数据,然后调用一个 .php 文件来写入文件。这是客户端与服务器端的问题,.php 使用 fopen、fwrite 和 fclose 解决了它。

回答by ModerateJavaScriptDev

Techically, when using javascript in a (.hta) file, you can use the ActiveXObject Scripting.FileSystemObjectto access the filesystem, with it there are all the commands you need to do anything to the filesystem that you have permission to do.

从技术上讲,在 (.hta) 文件中使用 javascript 时,您可以使用 ActiveXObjectScripting.FileSystemObject访问文件系统,有了它,您就可以对您有权执行的文件系统执行任何操作所需的所有命令。

var fso=new ActiveXObject("Scripting.FileSystemObject");
if(fso.FolderExists("C:\Users\")){
  alert("It exists, hello friend!");
}eles{
  alert("Doesn't exist, who are you?");
}