20

i want to create text file in local, when i browse in Google chrome click of the button it is showing error like ActiveXObject is not defined and when i browse in safari click of the button it is showing error like can't find variable: ActiveXObject . any one can help me.how can i achieve and create file .Thanq

<script>
      function createFile() {    
      var object = new ActiveXObject("Scripting.FileSystemObject");       
      var file = object.CreateTextFile("C:\\Hello.txt", true);
      file.WriteLine('Hello World');
      alert('Filecreated');
      file.WriteLine('Hope is a thing with feathers, that perches on the soul.'); 
      file.Close();
      }
    </script>
<input type="Button" value="Create File" onClick='createFile()'>
Ramesh Lamani
  • 1,087
  • 2
  • 25
  • 51

3 Answers3

27

ActiveXObject is available only on IE browser. So every other useragent will throw an error

On modern browser you could use instead File API or File writer API (currently implemented only on Chrome)

Fabrizio Calderan loves trees
  • 109,094
  • 24
  • 154
  • 160
11

ActiveXObject is non-standard and only supported by Internet Explorer on Windows.

There is no native cross browser way to write to the file system without using plugins, even the draft File API gives read only access.

If you want to work cross platform, then you need to look at such things as signed Java applets (keeping in mind that that will only work on platforms for which the Java runtime is available).

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
2

A web app can request access to a sandboxed file system by calling window.requestFileSystem(). Works in Chrome.

window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
var fs = null;

window.requestFileSystem(window.TEMPORARY, 1024 * 1024, function (filesystem) {
    fs = filesystem;
}, errorHandler);

fs.root.getFile('Hello.txt', {
    create: true
}, null, errorHandler);

function errorHandler(e) {
  var msg = '';

  switch (e.code) {
    case FileError.QUOTA_EXCEEDED_ERR:
      msg = 'QUOTA_EXCEEDED_ERR';
      break;
    case FileError.NOT_FOUND_ERR:
      msg = 'NOT_FOUND_ERR';
      break;
    case FileError.SECURITY_ERR:
      msg = 'SECURITY_ERR';
      break;
    case FileError.INVALID_MODIFICATION_ERR:
      msg = 'INVALID_MODIFICATION_ERR';
      break;
    case FileError.INVALID_STATE_ERR:
      msg = 'INVALID_STATE_ERR';
      break;
    default:
      msg = 'Unknown Error';
      break;
  };

  console.log('Error: ' + msg);
}

More info here.

jasssonpet
  • 2,009
  • 16
  • 17
  • thanks for your reply. i have used your code..on click of button it is giving. errorHandler is not defined. anything i have missed? – Ramesh Lamani Jun 20 '12 at 05:05