Can Javascript read a file to a string?

tscrap

New Member
Messages
14
Reaction score
0
Points
0
//God bless you!
I am searching for a JS function which does what the PHP function file() does. I want to read a file to a string or array. Is it possible with Javascript???
Thanks!
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
Copied from my post in your other thread:
Javascript can't read local files from the users computer unless you use a form with a file input and HTML5. Even then it is a bit hit and miss as some browser do not support the required functions.
If you want to load a file form the server you can use the XMLHttpRequest object and get the responseText or responseXML, depending on the format of the data.
Once you have the data loaded in either of these ways you can use arrays and string splitting just like you would in PHP, although Javascript is considerably less reliable and fast as PHP.
Where are you looking to load your file from, is it stored locally on your computer or is it on the server?
The data can be split into an array with a record for each line with
var fileArray = string.split('\n')
 
Last edited:

vishal

-::-X10 Guru-::-
Community Support
Messages
5,255
Reaction score
192
Points
63
**Moved To Programming Help**
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
Using that code, you'll want something like this:
HTML:
<script type="text/javascript">
var filePath = PATH_TO_FILE e.g. http://www.google.com/file.txt
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET",filePath,false);
xmlhttp.send(null);
var fileContent = xmlhttp.responseText;
var fileArray = fileContent.split('\n')
//Now do whatever you need with the array
</script>
That probably has a few errors, but I don't have time to check it.
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
The file you grab with a XMLHttpRequest has to be on the same domain as the page where you are making the request.
 
Messages
12
Reaction score
1
Points
0
....unless you make an ajax request to a php file that uses curl to load in the actual file.

Easiest way by far is to use Jquery to load the content from a local php file into the dom.
The local php file that is used in the AJAX call would have the ability to load content from remote sources.

( Reason I suggest Jquery, is that you will no longer need to mess around with browser sniffing and creating different types of objects, Jquery can do it with one call, e.g $('#result').load('ajax/test.html'); )
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Of course. I was pointing out the "security sandbox" because one of the examples given above was a file from google.com.

There are other ways around the restriction using dynamic script tags and JSON. You can get image lists from Flickr that way.

jQuery and Dojo are the easiest ways to do a lot of Ajax work. But it is nice to slog through the low level coding once to appreciate what a good library can do.
 
Messages
12
Reaction score
1
Points
0
absolutely descalzo...

To answer your question tscrap
a JS function which does what the PHP function file() does. I want to read a file to a string or array.

This can be complex to do, however if you utilise a framework such as Jquery it can be achieved with a single function call.

Firstly you will need a way to preserve the structure of data from php to javascript.
Use JSON (JavaScript Object Notation) http://en.wikipedia.org/wiki/JSON

Php supports JSON http://php.net/manual/en/ref.json.php
Jquery`s can make a single request to fetch JSON via AJAX and handle the complexitys http://api.jquery.com/jQuery.getJSON/
 
Top