Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript: import array.txt file and set to a variable importTXT

Status
Not open for further replies.

wrighterb

Programmer
Feb 20, 2007
80
US
I am trying to take the array.txt file that is on my server in the same folder of my jsp and import it, the set the contents of this text file to a javascript variable importTXT

This is what is in the txt file

[ "70950_0507","77-950-06-08","84-950-06-08","79-950-06-08" ] ;

an array of product numbers.

I have tried several things to no avail.
 
I have tried several things to no avail.

Perhaps you should list some of the things that you tried so that we don't waste your time posting a bunch of suggestions that you have already exhausted.

I can tell you now that it will likely involve using the FileSystemObject. Here's a tutorial on how it works using javascript:


-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
That is funny, I was fustrated and didn't add any code

<SCRIPT LANGUAGE="JavaScript">
<!--
var importTXT = new ActiveXObject("Scripting.FileSystemObject");
fileObj = importTXT.GetFile("array.txt");
-->
</SCRIPT>

then later I set aNames to importTXT

function createAutoComplete()
{
var aNames = importTXT;
new AutoComplete(
aNames,
document.getElementById('theText'),
document.getElementById('theDiv'),

That way my array is set to the Var aNames to complete the process
 
You are using the GetFile method on the FSO, instead you need to be using the OpenTextFile method (as shown in the link I posted above). Once you have done that you can use the ReadLine() method to pull data from the text file.

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
Shouldn't this
Code:
var aNames = importTXT
be
Code:
var aNames = fileObj;
?

Lee
 
<SCRIPT LANGUAGE="JavaScript">

var ForWriting = 2;
var TriStateFalse = 0;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var newFile = fso_OpenTextFile("array.txt",
ForWriting, true, TriStateFalse);

var importTXT = newFile.ReadLine();

so I have it like this and then use

var aNames = fileObj; ??

 
I was going by the last example you had provided. For accurate answers you have to show your most current code.

Lee
 
I also noticed that you're opening the file with a variable named ForWriting. How are you going to read the contents of the file?

Lee
 
ya I just copied those sorry,

<SCRIPT LANGUAGE="JavaScript">

var fso= new CreateObject("Scripting.FileSystemObject");
var myfile = fso_OpenTextFile("array.txt");

var importTXT = myfile.ReadLine();

Right now I am not really sure what I am doing.
 
<SCRIPT LANGUAGE="JavaScript">

var ForReading = 1;
var TristateUseDefault = -2;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var newFile = fso_OpenTextFile("array.txt", ForReading, true, TristateUseDefault);

var importTXT = newFile.ReadLine();

I am getting length 0 also this will only work in IE correct?
 
any time you see this:
Code:
var fso = new [!]ActiveXObject[/!]("Scripting.FileSystemObject");

it likely means it's only gonna work in IE

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
Read the javascript portion on this page:


It breaks it down to about as simple as it can get. Open a text file for writing. Write something to it. Close it. Reopen it for reading. Read something from it. Return the result.

I can only give one more suggestion - make sure you have the path to the text file correct so that the activex object knows where to find it.

If you're still having problems after reading the tutorial above I don't know that there's anything else we can do to help, other than write it for you.

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
Ok this is what I have

var fso, ts, s;
var ForReading = 1;
fso = new ActiveXObject("Scripting.FileSystemObject");
ts = fso_OpenTextFile("array.txt", ForReading);
s = ts.ReadLine();
Response.Write("File contents = '" + s + "'");
ts.Close();

Then later I call set aNames = s;
 
Echoing what kaht said, what is the path of the file you're trying to open?

Lee
 
Its in the same folder as this file.

Or do I need to put the whole path of the server?
 
>Its in the same folder as this file.
You don't use scripting.filesystemobject across http connection. That's it, period.
 
So there really isn't anyway to get the information from a text file. I know in flash you can read from a text file so I figured there must be a way in javascript.
 
>So there really isn't anyway to get the information from a text file.
I did not say exactly that, did I? You seem to like the look of the aj**. If the text file is on the server (not on the client), what hold you back from looking again at aj**? In any case, server can serve you the text file content via some markup pages like html. Do you know flash doing otherwise?

If the source file is on the client m/c and/or you want to persist some info as text file on the client m/c, sure scripting.filesystemobject is one candidate and you have to prepare to hurdle some security interfacing. In any case, summarying state of the art should not be done too lightly without making the conditions very clear.
 
I am trying to take the text file.

and create a Javascript Variable out of it's contents, then use that variable later in the Javascript.

What if I exported the information into a html page and then use a get url to place it into an array like so.

aNames = [ the Var of getURL ];

????

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top