Click to See Complete Forum and Search --> : Please help with code for remote file retrieving


xeen
05-04-2006, 06:56 PM
Hi. I am trying to write a short javascript that reads a text file from an ftp server and then simply displays the text inside that file. How can I do this? Thank you for any help.

phpnovice
05-04-2006, 09:18 PM
JavaScript cannot perform this function by itself. You'd need to use the XMLHttpRequest object to retrieve that text file. However, I believe you'd need server-side code to send the text file -- though I haven't verified whether or not direct calling of the text file itself (using the XMLHttpRequest object) will work.

mrhoo
05-04-2006, 09:41 PM
XMLHttpRequest is http only, doesn't work with ftp or file protocols.

You can simply load the file into an iframe on your page.
Use javascript to create the iframe or just to select the src-

phpnovice
05-04-2006, 09:45 PM
XMLHttpRequest is http only, doesn't work with ftp or file protocols.
That, I know. I presumed (perhaps incorrectly) that this FTP server would also be running a web server on it. If not, then the FTP process can probably be reconfigured to merely place the file on a web server for access as I previously described.

phpnovice
05-04-2006, 10:00 PM
By the way... Yes, an IFRAME would work, too. But, I've now verified that the XMLHttpRequest object can be used to retrieve text files without requiring the use of server-side code. The following worked perfectly for me (using my own function -- not for giveaway):
<div id="textOutput"></div>
<script type="text/javascript">
<!--//
makeXMLHttpRequest("http://www.domain.com/folder/some.txt");
document.getElementById("textOutput").innerHTML = xmlhttp.responseText;
//-->
</script>