Solved

Write to txt file using JavaScript

Posted on 2003-12-30
14
202,080 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

696 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