是否可以通过javascript访问本地文件? [英] Is it possible to access local file via javascript?

查看:1111
本文介绍了是否可以通过javascript访问本地文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



    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);
            }
    }

我收到错误:
自动化服务器无法在我创建ActiveXObject实例的行上创建对象


我知道使用javascript访问硬盘驱动器数据被认为非常糟糕,但我只需要它。 >
我正在使用 IE8,Greasemonkey4IE 运行我的javascript。

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.

谢谢,

Mohit < br>

Thank you,
Mohit

** * ** * ** * ** * ** * ** * ** * 的** * ** * ***

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



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

我将上面的代码放在一个简单的HTML页面中,它完美无缺。 >
http://www.c-point.com/JavaScript/ articles / file_access_with_JavaScript.htm

你可以在上面提到的位置找到类似的代码。

我修改了一下,很难。

但是当我试图通过 GreaseMonkey4IE 运行它时,它只是吐出我之前指定的相同错误。


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 GreaseMonkey4IE it simply spitting the same error I specified earlier.


我做过这些人,但非常感谢您的快速和有用的回复。

我所做的只是:

转到工具> Internet选项>安全性>自定义级别

在ActiveX控件和插件下,选择启用初始化和脚本未标记为安全的ActiveX控件。

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.

推荐答案

使用本机JavaScript,不,通常不可能访问本地文件。使用插件和扩展程序(如ActiveX,Flash或Java)可以解决此规则,通常会遇到一些困难。

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.

对于此一般规则的某些浏览器和操作系统特定的例外情况,你可能想看看这里:

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

使用javascript进行本地文件访问

请注意,截至2012年底, FileReader API ,并提供本机JavaScript机制来访问用户提名的 (通过输入元素或将它们放入浏览器中)。

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

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

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

这篇关于是否可以通过javascript访问本地文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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