开发者

Is it possible to access local file via 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);
            }
    }

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.

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开发者_JAVA技巧 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.


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.


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:

Local file access with 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.


HTML5 File API has multiple ways to access local files.

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

FileReader is 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.

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

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.


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 Enable for 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜