javascript 是否可以通过javascript访问本地文件?

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

Is it possible to access local file via javascript?

javascript

提问by EMM

    if (window.ActiveXObject) {
        try {
            var fso = new ActiveXObject("Scripting.FileSystemObject"); 
            fso.CopyFile("C:\Program Files\GM4IE\scripts\source.txt","C:\Program Files\GM4IE\scripts\target.txt", 1);
            fso = null;
            }
            catch (e) {
            alert (e.message);
            }
    }

I am getting error : "Automation server can not create object"on the line where I am creating ActiveXObject instance.
I understand that it's considered very bad to access hard-drive data using javascript but I just need it.
I am using IE8 , Greasemonkey4IE to run my javascript.

我在创建 ActiveXObject 实例的行上收到错误:“自动化服务器无法创建对象”
我知道使用 javascript 访问硬盘数据被认为是非常糟糕的,但我只是需要它。
我正在使用IE8、Greasemonkey4IE来运行我的 javascript。

Thank you,
Mohit

谢谢你,
莫希特

******************************

** *** *** *** *** *** *** *** *** ****

    function WriteFile()
    {
       var fso  = new ActiveXObject("Scripting.FileSystemObject");
       fso.CopyFile("C:\source.txt","C:\target.txt", 1);
    }

I've put the above code inside a simple HTML page and it worked perfect.
http://www.c-point.com/JavaScript/articles/file_access_with_JavaScript.htm
You can find the similar code on above mentioned location.
I modified it a bit, tough.
But when I am trying to run it through GreaseMonkey4IEit simply spitting the same error I specified earlier.


我已经把上面的代码放在一个简单的 HTML 页面中,它工作得很好。
http://www.c-point.com/JavaScript/articles/file_access_with_JavaScript.htm
您可以在上述位置找到类似的代码。
我修改了一下,很难。
但是当我尝试通过GreaseMonkey4IE运行它时,它只是吐出我之前指定的相同错误。




I did it guys, but thanks a lot for your quick and helpful replies.
All I did is :
Go to Tools > Internet options > Security > Custom Level
Under the ActiveX controls and plug-ins, select Enable for Initializing and Script ActiveX controls not marked as safe.

我做到了伙计们,但非常感谢您快速而有用的回复。
我所做的只是:
转至工具 > Internet 选项 > 安全性 > 自定义级别
在 ActiveX 控件和插件下,为初始化和脚本 ActiveX 控件选择启用未标记为安全。

回答by aroth

Using native JavaScript, no, it is not generally possible to access a local file. Using plugins and extensions like ActiveX, Flash, or Java you can get around this rule, generally with some difficulty.

使用原生 JavaScript,不,通常无法访问本地文件。使用 ActiveX、Flash 或 Java 等插件和扩展程序,您可以绕过这条规则,但通常会遇到一些困难。

For some browser and OS specific exceptions to this general rule, you might want to have a look here:

对于此一般规则的某些浏览器和操作系统特定例外,您可能需要查看此处:

Local file access with javascript

使用javascript访问本地文件

Note that as of late 2012, the FileReader APIhas been supported in all major browsers and provides a native JavaScript mechanism for accessing local files that the user nominates(via an inputelement or by dropping them into the browser).

请注意,截至 2012 年底,所有主要浏览器都支持FileReader API,并提供本机 JavaScript 机制来访问用户指定的本地文件(通过input元素或将它们放入浏览器中)。

This still cannot be used to access an arbitrary file by name/path as in the examples in the original question.

这仍然不能用于按名称/路径访问任意文件,如原始问题中的示例所示。

回答by Raynos

HTML5 File API has multiple ways to access local files.

HTML5 File API 有多种访问本地文件的方式。

window.requestFileSystemallows you to request access to the filesystem. Browser support is very poor on this (Chrome only).

window.requestFileSystem允许您请求访问文件系统。浏览器对此的支持非常差(仅限 Chrome)。

FileReaderis the HTML5 FileReader API that allows you to programatically read files that users select through a <input type='file' />Browser support is better on this.

FileReader是 HTML5 FileReader API,它允许您以编程方式读取用户通过<input type='file' />浏览器选择的文件,对此支持更好。

You should use fallbacks like flash and POST to a server for full file access.

您应该使用闪存和 POST 等回退到服务器以进行完整的文件访问。

Generally reading arbitary files is considered "cheating the browser" so I you'll either have to use secure HTML5, ActiveX or Flash. All 3 of those require user permissions.

通常阅读任意文件被认为是“欺骗浏览器”,因此您必须使用安全的 HTML5、ActiveX 或 Flash。所有这三个都需要用户权限。

回答by EMM

After some research I have found:

经过一番研究,我发现:

 var fso = new ActiveXObject("Scripting.FileSystemObject");
 //This line will create a xml file on local disk, C drive
 fh = fso.CreateTextFile( "C:\fileName.xml", true);
 fh.WriteLine("this is going to be written in fileName.xml");

This is how we can do it.There are other methods also.

这就是我们可以做到的。还有其他方法。

Accessing local file system is very bad thing to do but yes we can do it.

访问本地文件系统是一件非常糟糕的事情,但是我们可以做到。

Automation server can not create object

自动化服务器无法创建对象

To get rid of this error go to Tools → Internet Options → Security → select Internet icon → click Custom level → select Enablefor Initialize and script ActiveX controls not marked as safe for scripting.

I have not tested this on any other berowser except IE8, but I am sure it will work.

要消除此错误,请转至工具 → Internet 选项 → 安全性 → 选择 Internet 图标 → 单击自定义级别 →为初始化和脚本启用未标记为脚本安全的 ActiveX 控件。

我没有在 IE8 之外的任何其他浏览器上测试过这个,但我相信它会工作。