-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") ?

isherwood
  • 58,414
  • 16
  • 114
  • 157

1 Answers1

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!

NMunro
  • 890
  • 5
  • 20