• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2024
  • Last Modified:

Reading a file using javascript

I am trying to read a file from the web server using javascript.

I know client side javascript can't read a file so what i'm trying is to have the web page activate a javascript on the server.

The web page looks like:

<HTML>

<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-8">
</HEAD>

<BODY bgcolor=66FFFF bgproperties="fixed">


<br>

<script language="javascript" src="javascript.js"></script>



</BODY>
</HTML>

On the web server i have this file

javascript.js:

x = new File("c:\temp\userInfo.txt")
if (x.open("r"))
{
      while (!x.eof())
      {            line = x.readln()
            write(line+"<br>");
      }
x.close();
}

But it doesn't work, Can anyone realize why?
Thanks
0
shlomi
Asked:
shlomi
  • 5
  • 4
1 Solution
 
mattjp88Commented:
To answer your question i one word, No, you can't read anything on the server (except .js files) with javascript.  You also can't have javascript activate on the server.  MS Frontpage has server side functions, but I personally don't trust them.  The best option would be to use PHP to read the file and out put each line induvidually.  If you need a PHP script to do that, I would be ahppy to provide one.

-Matt
0
 
ZontarCommented:
You can do this using PHP or ASP, and I suspect most if not all other serverside programming languages.

What sort of info is in the file, and how do you intend to use this info clientside?
0
 
ZontarCommented:
Or are you just wanting to print out part of another file in your HTML? A servserside include might also provide a solution.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
shlomiAuthor Commented:
How come http://docs.sun.com/source/816-6410-10/file.htm says it's possible with server side javascript.

Am i missing something?

0
 
shlomiAuthor Commented:
Here is what i'm trying to do:

I want to have a marquee that includes text coming from this file.
First i have to read the file into an array, than i have to display each variable of the array.
0
 
ZontarCommented:
Serverside JavaScript and clientside JavaScript have absolutely nothing to do with each other, except that the core language is the same -- the programming environments and available objects are completely different. Clientside JavaScript has no access to the filesystem, except you can include .js files -- but these must consist of clientside code.

What's running on your server? If you have ASP, you can do this -- read a file one line at a time, write a clientside JS array declaration in which each line from the file is written as an element in the array. The important thing to note is that we use Response.ContentType to send a "Content-Type: text/javascript" header to the browser before actually writing the output so the browser will in fact interpret the text as JavaScript and not plaintext.

Source of marquee-txt.asp:

<%@LANGUAGE="VBSCRIPT"%>
<%
  Set FSO = Server.CreateObject("Scripting.FileSystemObject");
  Set File = FSO.GetFile("C:\TEMP\userInfo.txt")
  File.OpenAsTextStream(1, 0)

  Output = "var myArray = ["

  Do While Not File.AtEndOfStream
    Line = File.ReadLine
    Output = """" & Line & """, " & Output
  Loop

  Output = Left(Output, Len(Output) - 1)
  Output = "];" & Output

  Response.ContentType = "text/javascript"
  Response.Write Output
%>

Now in your HTML file, just use

<script type="text/javascript" src="marquee-txt.asp"></script>

and the browser will treat the output of this ASP file (not the ASP code, but the text generated by the ASP code) as though it were the content of a .js file.

As I said, the same thing can be done with PHP. What do actually you have running on your server?
0
 
shlomiAuthor Commented:
Zontar
Thanks for the answer, However i think i am a little mixed up.
I understand that Javascript has two braches: server and client.

And i know that the server side language can handle readina  local file on the server.

Why is it that The script you wrote is VB?
Also, In regarding to ASP: Is it a language or just a mechanisem to produce dynamic content ?

I am running Apache (Does it support VB, Javascript?).

Thanks
0
 
ZontarCommented:
ASP is a programming environment. It supports both VBScript and JScript "out of the box" and other scripting languages (Perl and Python among themsee www.activestate.com) can be installed to work with it. Most ASP programmers use VBScript, but I much prefer JScript (I'm better with JS, and it's a more flexible/powerful language IMO). I used VBScript in my example so as not to add to the confusion.

ASP/ JScript version of the above:

<%@LANGUAGE="JSCRIPT"%>
<%
  var fso = Server.createObject("Scripting.FileSystemObject");
  var file = fso.getFile("C:\TEMP\userInfo.txt");
  file.openAsTextStream(1, 0);

  var output = "var myArray = [";
  var line;

  while(!file.atEndOfStream)
  {
    line = file.readLine();
    output += "\"" + line + "\", ";
  }

  file.close();

  output = output.slice(0, -1) + "];";

  Response.contentType = "text/javascript";
  Response.write(output);
%>

Apache doesn't support ASP, only MS-IIS does that (unless you want to buy a fairly expensive ASP clone from Sun). If you're running Apache, my suggestion would be to install PHP. It's free, a fairly easy install, it's a heck of a lot easier to learn quickly than either Perl or Python, and works great on Windows (I have two Windows servers running here, one using IIS and one using Apache, both running PHP). To use SunONE SSJS, you'd have to install their server -- I know Netscape used to have a free developer verison, but I never installed it myself as it was a pretty big download.

PHP version:

<?php
  $fp = fopen("C:\\TEMP\\userInfo.txt");

  $output = "var myArray = [";

  while( !feof($fp) )
  {
    $line = fgets($fp, 1024);
    $output .= "\"$line\"',";
  }

  fclose($fp);

  $output = substr($line, 0, -1) . "];"

  header("Content-Type: text/javascript");
  echo $output;
?>

It's possible there might be a glitch or two in my examples -- I wrote them all off the top of my head -- but the same basic concepts apply in each, and with a little tweaking, any of them should work in the right environment.
0
 
shlomiAuthor Commented:
Thanks a lot,
You have clearly made is simpler for me.
Shlomi
0
 
ZontarCommented:
No worries.

I started using this technique in PHP a while back to generate JS and CSS files from info stored in a MySQL DB.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now