-2

I Got this code to create a file using JavaScript.

 function WriteToFile()
        { 
            var txt = new ActiveXObject("Scripting.FileSystemObject");
            var s = txt.CreateTextFile("11.txt", true);
            s.WriteLine('Hello');
            s.Close();
         }
WriteToFile();

it is working fine with IE but not Working in Chrome and Firefox. usually the error is ReferenceError: ActiveXObject is not defined is there any alternate to create file in javascript, instead of ActiveXobject(Scripting.FileSystemObject") ?

5
  • Why not use NodeJS, you awill be able to require the File System core module and just create it using javascript as well Commented Nov 30, 2015 at 11:02
  • 3
    Possible duplicate of ActiveXObject in Firefox or Chrome (not IE!) Commented Nov 30, 2015 at 11:02
  • What do you mean by "Create file using JavaScript"? Do you want a user to download the dynamically generated file? Or how are you going to work with this file? Commented Nov 30, 2015 at 11:03
  • 2
    is there any alternate to create file in javascript - no, web pages can not create a file on the local filesystem, thankfully Commented Nov 30, 2015 at 11:04
  • not download.it is going to save the file @ perticular location. Commented Nov 30, 2015 at 11:05

1 Answer 1

1

This isn't possible. At least not in pure JavaScript anyway, ActiveX objects are part of the Windows/IE thing and are NOT in the ES6 spec (http://www.ecma-international.org/ecma-262/6.0/index.html), which is why they're not supported in Chrome or Firefox.

If you wish to write JavaScript programs that can create files on the users local file system you're going to need to write a client side app, such as a chrome app (https://developer.chrome.com/apps/api_index) or write your program using Electron shell(http://electron.atom.io/).

However, if what you want is a cross browser solution to create files at specific locations on a user file system, it's just not really natively possible.

Sure browser plugins exist, but you'd need every user to install the plugin, so that's not exactly practical. For security reasons websites aren't allowed to read the users hard drive, and this is a good thing!

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.