本地 JavaScript - 写入本地文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5349921/
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
Local JavaScript - write to local file
提问by Jus12
I have some JavaScript code loaded from a local HTML file (without going through a webserver).. i.e., opened using file://
我有一些从本地 HTML 文件加载的 JavaScript 代码(不通过网络服务器).. 即,打开使用 file://
Is there a way the JavaScript code in this file can be used to write to a local file?
有没有办法可以使用此文件中的 JavaScript 代码写入本地文件?
I know that cross-site restrictions do not allow lot of things in JavaScript, but this is not cross-site, so it should be allowed in theory.
我知道跨站限制不允许在 JavaScript 中做很多事情,但这不是跨站的,所以理论上应该是允许的。
采纳答案by Diogo Cardoso
There's a jQuery plugin jQuery.twFilethat allows you to read and write to a local file.
有一个 jQuery 插件jQuery.twFile允许您读取和写入本地文件。
回答by Ashwin Krishnamurthy
In case of Internet Explorer you can use ActiveX.
在 Internet Explorer 的情况下,您可以使用ActiveX。
<html>
<head>
<script type="text/javaScript">
function WriteToFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var txtFile = fso.CreateTextFile("c:\TestFile.txt", true);
txtFile.WriteLine("This is a test");
txtFile.Close();
}
</script>
</head>
<body>
<p>
<script type="text/javaScript"> WriteToFile(); </script>
</p>
</body>
</html>
回答by Lee Kowalkowski
There's no native API* for File System access in browsers. You need that first!
浏览器中没有用于文件系统访问的本机 API*。你首先需要那个!
For example, in Internet Explorer, there's an ActiveX object for it:
例如,在 Internet Explorer 中,它有一个 ActiveX 对象:
var fso = new ActiveXObject("Scripting.FileSystemObject");
...but it requires the user to relax their browser settings. On other browsers, you may be able to use a jar (Java Archive) file.
...但它需要用户放松他们的浏览器设置。在其他浏览器上,您可以使用 jar(Java 存档)文件。
You could also see what non-browser JavaScript containers offer, e.g. Microsoft HTA files (HTML Application) will support the Windows ActiveX FileSystemObject fine, providing your Virus Checking Software allows HTA files to execute.
您还可以查看非浏览器 JavaScript 容器提供的内容,例如 Microsoft HTA 文件(HTML 应用程序)将很好地支持 Windows ActiveX FileSystemObject,前提是您的病毒检查软件允许执行 HTA 文件。