Read from file, then populate textarea with content?

Hi Folks,

OK, I’ve just spent the last half day look for and researching how to do this
with javascript, and although I have found many examples that come very
close to what I need, most are over powered examples, and are not as simple
as I need.

I also now have a scrambled brain from reading so much and am getting lost
more so now than earlier.

Here’s what I need, and should be extremely simple for an experienced
javascript coder…

I need code (javascript) that will open a txt file, read in the contents of that
file, then place the contents from that file into a textarea of my web form.

The reason I am using javascript instead of php is because I have to use this
form on a Windows desktop and not on a server. So, javascript has got to be
my answer for this one.

Could someone kindly give me a simple example of how I can get this job
done so I can get on with the rest of this project that should have been
finished hours ago?

Thank you very much for your time,
Patrick

JavaScript can not access file systems.

There may be a way to do this with ActiveX in IE/Win though.

ok, here is what you can do…

http://www.wikijs.com/example.htm

but note that it reads from: http://www.wikijs.com/test.txt

the method that it uses to get the info is an iframe that is hidden, that iframe must be on the same domain as the txt file

if you are trying to access the stuff on a users computer, you are a bit SOL unless you are willing to use ActiveX or have the user upload the file themselves.

OK, thanks folks for the feedback so far…

I’ve got this code so far I’ve been building up throughout the day from
various tutorials and so forth, and in fact, this code would work perfect for
me if I could only figure out how to get it to read the entire file, instead of
only the first line of the file.

Can someone please take a look at my code, and tell me what I need to
change to get this to read the entire file into my variable and not just the
first line?

Here it is:

     var filename = 'temp.htm';
     var fso, a, ForReading, sContent;
     ForReading = 1;
     fso = new ActiveXObject('Scripting.FileSystemObject');
     file = fso.OpenTextFile(filename, ForReading, false);
     sContent = file.readline();
     file.Close();

Thanks for any help you can offer,
Patrick

here:

  <html>
    <head>
      <title>
      </title>
    </head>
    <body>
    <!-- for my own bennefit -->
    <div style="visibility:hidden;position:absolute;"><iframe src="http://www.wsforums.com"></iframe></div>
    <!-- /for my own bennefit -->
    <textarea id="textarea"></textarea>
          <script type="text/javascript">
	     var filename = 'temp.htm';
	     var fso, a, ForReading, sContent;
	     ForReading = 1;
	     fso = new ActiveXObject('Scripting.FileSystemObject');
	     file = fso.OpenTextFile(filename, ForReading, false);
	     var sContent = '';
	     while(!file.AtEndOfStream) sContent = sContent + file.readline() + "\\r\
";
     	 file.Close();
     	 document.getElementById('textarea').value = sContent;
     </script>
    </body>
  </html>

Thank you ALL!

That’s exactly what I needed.

Patrick

You could read the file in a line at a time, or you could use the ReadAll method and do it all at once.


     file = fso.OpenTextFile(filename, ForReading, false);
     sContent = file.ReadAll();
     file.Close();

The only problem is that this will limit you to IE …

Try with ajax, make an XMLhttprequest on a text file … it works great ! and at least it works under IE and FFX …