Forum Moderators: open

Message Too Old, No Replies

file handling in javascript

         

gilbeccu

1:34 pm on Jun 17, 2004 (gmt 0)



Hi, I need to load a txt file in a javascript script in IE. Then I have to create, always in the script, an xml document obtaining the data from the txt file.
I only know basic javascript and xml dom. Can you help me?
Thanks a lot!
Bye!

MichaelBluejay

8:38 pm on Jun 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



JavaScript can't read or write files because it's a security risk. If it could then anyone with a web browser could read and write files on your system. That's because JavaScript is client-side, meaning it's executed by the browser.

What you want is a server-side language, like Perl or PHP. That code gets executed by the server, not by your web browser.

DrDoc

12:47 am on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, JavaScript can read files, in roundabout ways. The easiest way to read a file is through an ActiveX or Java object. Though this is not as simple as "open/read/write/close", it is still possible.

A quick Google search [google.com] reveals some of the possibilities, but also the many limitations.

In general, it is best if file reading is handled on the server. JavaScript cannot be used to read local files on the computer.

Bernard Marx

1:25 am on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm. All of this depends...

Granted, this might be done better on the server, if that is possible, but gilbeccu hasn't said that he wants to read files locally. Reading text files (on your own domain) from a web page with JS is possible, and can be relatively easy. Reading local files is also possible with JS (as JScript) if you are running it as an HTA.

Equally, gilbeccu hasn't said that he wants to save this XML document as a file (yet). So "no" is a little premature there too.

We need those requirements filled out a bit, along with things like the format of the text file, and what information it contains

------ Easiest text file reading for IE (within domain) ---------

Include this element:

<IE:Download id = "mydata" style = "behavior:url(#default#download)" />

Use this script:

window.onload = function()
{
document.getElementById("mydata").startDownload("textfile.txt",handleData)
}

function handleData(text)
{
alert(text)
}

DrDoc

2:49 am on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, it's not too hard to accomplish in IE. But if you're looking for a more stabile cross-browser solution it's not all that simple.

Bernard Marx

8:27 am on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Fortunately:
I need to load a txt file in a javascript script in IE