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


Solution 1:

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.

Solution 2:

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.