Reading a data file in JavaScript

H

HardySpicer

I have a cvs data file orbit.csv and found this code.

function ReadFile()
{
var fso, f1, ts, s;
var ForReading = 1;
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso_OpenTextFile( Server.MapPath("orbit.csv"), ForReading);
return(f.ReadAll());
}

The data file has four columns and the first line maybe something like

1.23,3.45,6.78,2.34

I need access to the second and third column only. Can I use teh above
function - if so how exactly?

Thanks

Hardy
 
T

Thomas 'PointedEars' Lahn

HardySpicer said:
I have a cvs data file orbit.csv and found this code.

function ReadFile()
{
var fso, f1, ts, s;
var ForReading = 1;
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso_OpenTextFile( Server.MapPath("orbit.csv"), ForReading);
return(f.ReadAll());
}

The data file has four columns and the first line maybe something like

1.23,3.45,6.78,2.34

I need access to the second and third column only. Can I use teh above
function - if so how exactly?

RTFM:

http://msdn2.microsoft.com/en-us/library/314cz14s(VS.85).aspx
http://msdn2.microsoft.com/en-us/library/312a5kbt.aspx
http://msdn2.microsoft.com/en-us/library/t58aa4dd(VS.85).aspx
http://msdn2.microsoft.com/en-us/library/t5az126b(VS.85).aspx
http://msdn2.microsoft.com/en-us/library/tkcsy6fe(VS.85).aspx


PointedEars
 
D

Doug Gunnoe

I have a cvs data file orbit.csv and found this code.

function ReadFile()
{
  var fso, f1, ts, s;
  var ForReading = 1;
  fso = new ActiveXObject("Scripting.FileSystemObject");
     f = fso_OpenTextFile( Server.MapPath("orbit.csv"), ForReading);
  return(f.ReadAll());

}

The data file has four columns and the first line maybe something like

1.23,3.45,6.78,2.34

I need access to the second and third column only. Can I use teh above
function - if so how exactly?

Thanks

Hardy

Do you have some need to do this in JavaScript? Could you elaborate a
little more in what context you will be doing this?

Local file access form a browser is typically a no-no. ActiveX will
only work in IE, and at a minimum the user would be prompted to allow
or deny active x access to the file system.

And I have no idea what the "Server" object referenced here is
'Server.MapPath("orbit.csv")'

As far as accessing the 2nd and third columns, there are many ways
this can be done. You could read the whole file line by line, put each
line into a string, and parse the string using various string
functions, keying on the commas. Since it is a csv file, it can be
probably read in as a dataset, or recordset..oledb comes to mind. At
any rate, I would not use JavaScript for this.
 
H

HardySpicer

Do you have some need to do this in JavaScript? Could you elaborate a
little more in what context you will be doing this?

Local file access form a browser is typically a no-no. ActiveX will
only work in IE, and at a minimum the user would be prompted to allow
or deny active x access to the file system.

And I have no idea what the "Server" object referenced here is
'Server.MapPath("orbit.csv")'

As far as accessing the 2nd and third columns, there are many ways
this can be done. You could read the whole file line by line, put each
line into a string, and parse the string using various string
functions, keying on the commas. Since it is a csv file, it can be
probably read in as a dataset, or recordset..oledb comes to mind. At
any rate, I would not use JavaScript for this.

Ok I need to plot the GPS orbit of a satellite. I have the data and I
want to use Google Maps. I know how to plot a line in Google Maps no
problem but I need to read the orbit data line by line then plot teh
data. Of course I can do this locally but I want others to get access
and at present the data is only simulated - it will be real at some
point.

I was also unsure how to access the data file: Microsoft gives this

a = fs.OpenTextFile("c:\\testfile.txt", ForAppending, false);

but what does this mean on a server? (ie the path is ??) The data is
on the server and not on my pc. Not worried about acknowledging active-
x btw - that's ok.

Hardy
 
D

Doug Gunnoe

Ok I need to plot the GPS orbit of a satellite. I have the data and I
want to use Google Maps. I know how to plot a line in Google Maps no
problem but I need to read the orbit data line by line then plot teh
data. Of course I can do this locally but I want others to get access
and at present the data is only simulated - it will be real at some
point.

I was also unsure how to access the data file: Microsoft gives this

a = fs.OpenTextFile("c:\\testfile.txt", ForAppending, false);

but what does this mean on a server? (ie the path is ??) The data is
on the server and not on my pc. Not worried about acknowledging active-
x btw - that's ok.

Hardy- Hide quoted text -

- Show quoted text -

Since the file is on a server, an activeX file system object will not
work. It would just try to access the user's system.

You could use a server side script to read the file and parse the
values you need. I know PHP has functions to parse and use CSV files.
This would be an easier way to handle the problem, IMO.

http://us.php.net/fgetcsv

And I'm sure other server side technologies do as well.

You can also use AJAX

http://developer.mozilla.org/en/docs/AJAX:Getting_Started
 
T

Thomas 'PointedEars' Lahn

Doug said:
Ok I need to plot the GPS orbit of a satellite. I have the data and I
want to use Google Maps. I know how to plot a line in Google Maps no
problem but I need to read the orbit data line by line then plot teh
data. Of course I can do this locally but I want others to get access
and at present the data is only simulated - it will be real at some
point.

I was also unsure how to access the data file: Microsoft gives this

a = fs.OpenTextFile("c:\\testfile.txt", ForAppending, false);

but what does this mean on a server? (ie the path is ??) The data is
on the server and not on my pc. Not worried about acknowledging active-
x btw - that's ok.
[...]

If you Google idio^W users would ever learn how to quote properly?
Since the file is on a server, an activeX file system object will not
work.

Yes, it will.
It would just try to access the user's system.

It will allow access to the filesystem of the computer it is being created on.

http://msdn2.microsoft.com/en-us/library/d6dw7aeh(VS.85).aspx


PointedEars
 
T

Thomas 'PointedEars' Lahn


By trimming too much, you have been destroying the context of the quoted
text here. Will you ever learn how to quote properly?
From client side javascript?

Nobody aside from you ever stated a restriction of a solution to client-side
JavaScript, let alone JavaScript instead of JScript. In fact, the OP
specifically asked about server-side JScript in
<8ed3c0d3-2e55-45dd-8f42-998e542c658d@r60g2000hsc.googlegroups.com>.

You should read more thoroughly and be slower to jump to conclusions; that
would avoid such misunderstandings.


PointedEars
 
D

Doug Gunnoe

 Will you ever learn

Probably not.
Nobody aside from you ever stated a restriction of a solution to client-side
JavaScript,

Well if that is the case, all apologies to the OP.

(But I would still consider using something other than J\JavaScript
and a file system object for this.)
 
H

HardySpicer

Since the file is on a server, an activeX file system object will not
work. It would just try to access the user's system.

You could use a server side script to read the file and parse the
values you need. I know PHP has functions to parse and use CSV files.
This would be an easier way to handle the problem, IMO.

http://us.php.net/fgetcsv

And I'm sure other server side technologies do as well.

You can also use AJAX

http://developer.mozilla.org/en/docs/AJAX:Getting_Started

Yes I solved the problem in PHP but the map wasn't good enough -I
needed a zoom facility.
I then had the problem of how to combine PHP with Javascript (since
Javascript is used for Google Maps). I am not sure how to do this. I
need to read a line of data then plot. So I need PHP followed by
Javascript then repeat.

Hardy
 
D

Doug Gunnoe

Yes I solved the problem in PHP but the map wasn't good enough -I
needed a zoom facility.
I then had the problem of how to combine PHP with Javascript (since
Javascript is used for Google Maps). I am not sure how to do this. I
need to read a line of data then plot. So I need PHP followed by
Javascript then repeat.

Hardy- Hide quoted text -

- Show quoted text -

If you need to read the file - plot, read the file - plot because the
file is changing after the browser has loaded the page, then something
like AJAX would be a good choice, and plus it uses JavaScript like
Google maps (which is also an AJAX app I think).

But if the orbit.csv file does not change whilst the user has loaded
your page, then PHP can work with google maps, simply by writing
JavaScript vars with php.

For example, and consider this more or less pseudocode, and this can
be done many different ways

<script>
function plotStuff(){
var plotData = [];
for(var i = 0;i<something;i++){ //or some appropriate loop here
plotData = <?php echo some_PHP_function('read the data') ?>;
}

</script>

where some_PHP_function would read the data you want to read.

Of course, there are many different ways to do this, you could read
everything into an array in PHP first, and then for each item in the
array do

plotData = <?php echo plotDataPHP ?>
And again, this is more or less pseudocode, so don't take it
literally, but the general idea will work.

After this, you have your info into javaScript and can do as you
please with it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top