0

Hello i have had problems with my html/javascript code:

function rf()
{

    var fs,file;

    fs = new ActiveXObject('Scripting.FileSystemObject');

    file = fs.OpenTextFile('/test123.txt',2);

    file.Write('The text to write to file');

    file.Close();

}

</script>
<button onclick="rf()">Try it</button>
</html>

i go on the page, click the button, no success. can someone correct this for me?

3
  • 2
    This only works in Internet Explorer and not in other browsers. Commented May 5, 2013 at 19:58
  • You should use AJAX for this and let your server-side code handle the file-handling. Commented May 5, 2013 at 20:00
  • And in IE only if the security settings are low enough. Commented May 5, 2013 at 20:00

1 Answer 1

1

Even Internet Explorer does not allow this construct by default, in any version - you'll need to manually switch some very dangerous settings to allow this. The whole purpose of Javascript in a browser is that it is sandboxed inside the browser process, and has no means at all of accessing, or even worse writing to, the surrounding computer and its filesystems. If this code would work, what would stop someone from doing the same with files inside your System32 folder? Or hiberfil.sys? Or autoexec.bat? Needless to say, what you are trying to achieve cannot and should not ever work, on any computer, in any browser.

Microsoft documentation on the subject:

Because using the FSO on the client side may provide potentially unwelcome access to a client's local file system, you should use it only in scripts executed on the server side. Internet Explorer default security settings do not allow client-side use of the FileSystemObject object. Overriding those defaults could subject a local computer to unwelcome access to the file system, which could result in total destruction of the file system's integrity, causing loss of data, or worse.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.