Solved

Write to txt file using JavaScript

Posted on 2003-12-30
14
201,990 Views
Last Modified: 2011-08-18
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....
0
Comment
Question by:itobie
  • 2
  • 2
  • 2
  • +7
14 Comments
 
LVL 4

Accepted Solution

by:
ZeroPage earned 250 total points
ID: 10015187
That page where the use is working offline is a html page stored on users disk, right?
Rename that page file extesnsion from .htm to .hta and it will work.

Good luck,
ZeroPage

0
 
LVL 14

Expert Comment

by:avner
ID: 10015333
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.
0
 
LVL 4

Expert Comment

by:ZeroPage
ID: 10016135
It will only work on local file.
It will not work on web server, neither Intra nor Internet.

0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 14

Expert Comment

by:avner
ID: 10016172
ZeroPage, This IS working on web-server.
0
 
LVL 10

Expert Comment

by:Nushi
ID: 10016223
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>
0
 
LVL 7

Expert Comment

by:TheKenman
ID: 10209871
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).
0
 
LVL 1

Expert Comment

by:rksprst
ID: 11484421
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
0
 
LVL 7

Expert Comment

by:TheKenman
ID: 11487480
rksprst,

You need to open a new question for each question that you have.
0
 

Expert Comment

by:bhubesh_be
ID: 12461884
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.
0
 

Expert Comment

by:riekey
ID: 12716715
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>
0
 

Expert Comment

by:riekey
ID: 12716736
btw, no file was created.
I want the code to write to and read from a file on server, what should i do?
0
 
LVL 1

Expert Comment

by:samme
ID: 13203018
I cannot get this code to work either.  Can someone please help?
0
 

Expert Comment

by:sasisuman
ID: 21331153
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
0
 

Expert Comment

by:dolbyarun
ID: 23733163
Ok this works for IE. How to do the same with FireFox?
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question