Link to home
Start Free TrialLog in
Avatar of itobie
itobie

asked on

Write to txt file using JavaScript

Hi,

I would like to know if it is possible to use JavaScript to write to a text file. I tried using FileSystemObject in JavaScript. However, it gives the following error : "Automation server can't create object". Does anyone know what is going wrong here?

To give an overview of what I am trying to achieve here. There are 2 parts I need to do.

1) enable users read information from an XML file offline. I managed to user JavaScript to read
    and display the necessary informatiom (for example : a quiz).

2) read quiz result from the page and write to a text file. Upon going online, there shall be a
   function to read the result from the text file and update it into our DB.

2nd part is an issue to me now. If you think this is not feasible, could anyone suggest any other
ways to work around this?

Attached is the code for the 2nd portion :

========================================================
function WriteToFile() {
  try {
    var fso, s;
    fso = new ActiveXObject("Scripting.FileSystemObject");
    s = fso.CreateFolder("C:\\test.txt", true);
    s.writeline("This is a test");
    s.Close();
  }
 catch(err){
   var strErr = 'Error:';
   strErr += '\nNumber:' + err.number;
   strErr += '\nDescription:' + err.description;
   document.write(strErr);
  }


Thanks a lot....
ASKER CERTIFIED SOLUTION
Avatar of ZeroPage
ZeroPage

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of avner
avner

This worked for me :

function WriteToFile() {
 try {
   var fso, s;
   fso = new ActiveXObject("Scripting.FileSystemObject");
   s = fso.OpenTextFile("C:\\test.txt" , 8, 1, -2);
   s.writeline("This is a test");
   s.Close();
 }
catch(err){
  var strErr = 'Error:';
  strErr += '\nNumber:' + err.number;
  strErr += '\nDescription:' + err.description;
  document.write(strErr);
 }
}

But I think it will only work on Intranet.
It will only work on local file.
It will not work on web server, neither Intra nor Internet.

ZeroPage, This IS working on web-server.
as avner posted his solution will work.
mine is very similar but gives you a feedback if the file was succesfully
created /written or not.

Nushi.

<html><head>
<SCRIPT LANGUAGE='JavaScript'>
var password='theirpassword';
var name='username';

function WriteToFile() {
      var filename = 'c://temp.txt';
      var fso = new ActiveXObject('Scripting.FileSystemObject');
      if (fso.FileExists(filename)) {
            var a, ForAppending, file;
            ForAppending = 8;
            file = fso.OpenTextFile(filename, ForAppending, false);
            file.WriteLine(name);
            file.WriteLine(password);
            }
      else {
            var file = fso.CreateTextFile(filename, true);
            file.WriteLine(password);
            file.WriteLine(name);
            }
      file.Close();
      }

function ReadIt() {
      var filename = 'c://temp.txt';
      if (confirm('Do you want to see what we put on your computer?')) {
            var fso, a, ForReading;
            ForReading = 1;
            fso = new ActiveXObject('Scripting.FileSystemObject');
            file = fso.OpenTextFile(filename, ForReading, false);
            var name = file.readline();
            var password = file.readline();
            file.Close();
            document.write(name + '<br>');
            document.write(password);
      }
}
</SCRIPT>
</head>
<body onload='WriteToFile();ReadIt()'>
</body>
</html>
I've experimentec with this before, it could also have to do with your browser's security settings. When I was testing it would prompt me to approve the activeX application (thus the reason a .hta would work.. no restrictions on security settings).
do u guys know how i can format a text file? for example when i write to it i want to add tabs,, spaces...etc.

also how do i start writing at a specific line which i specify?

TIA,
alex
rksprst,

You need to open a new question for each question that you have.
hi to all,
          another important question.
          how to validate a html input file using javascript having errors like multiple slashes
          from my program(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Es2\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\bhupesh\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\mercur_files\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\bayshore.jpg)
                please help as soon as possible.
Nushi, your code below does not work on either my pc  or my department pc. I tried to add some alerts into the code to find out where the problem is. i found that code after "var fso = new ActiveXObject('Scripting.FileSystemObject');" doesn't called. I put an alert after that line but it didn't come up. Any idea?

<html><head>
<SCRIPT LANGUAGE='JavaScript'>
var password='theirpassword';
var name='username';

function WriteToFile() {
     var filename = 'c://temp.txt';
     var fso = new ActiveXObject('Scripting.FileSystemObject');
     if (fso.FileExists(filename)) {
          var a, ForAppending, file;
          ForAppending = 8;
          file = fso.OpenTextFile(filename, ForAppending, false);
          file.WriteLine(name);
          file.WriteLine(password);
          }
     else {
          var file = fso.CreateTextFile(filename, true);
          file.WriteLine(password);
          file.WriteLine(name);
          }
     file.Close();
     }

function ReadIt() {
     var filename = 'c://temp.txt';
     if (confirm('Do you want to see what we put on your computer?')) {
          var fso, a, ForReading;
          ForReading = 1;
          fso = new ActiveXObject('Scripting.FileSystemObject');
          file = fso.OpenTextFile(filename, ForReading, false);
          var name = file.readline();
          var password = file.readline();
          file.Close();
          document.write(name + '<br>');
          document.write(password);
     }
}
</SCRIPT>
</head>
<body onload='WriteToFile();ReadIt()'>
</body>
</html>
btw, no file was created.
I want the code to write to and read from a file on server, what should i do?
I cannot get this code to work either.  Can someone please help?
Iam just wondering about the security level of the IE set on your box. i got the same error but when i changed the security setting in IE options it worked like heaven for me
Ok this works for IE. How to do the same with FireFox?