Can Javascript read a file to a string?

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by tscrap, May 20, 2010.

  1. tscrap

    tscrap New Member

    //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!
  2. lemon-tree

    lemon-tree x10 Minion Community Support

    Copied from my post in your other thread:
    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
    Last edited: May 20, 2010
  3. galaxyAbstractor

    galaxyAbstractor Community Advocate Community Support

  4. lemon-tree

    lemon-tree x10 Minion Community Support

    You'll want to use the 'For Netscape 6' one from that page, the other two use techniques that have been superseded and do not work on all browsers.
  5. vishal

    vishal -::-X10 Guru-::- Community Support

    **Moved To Programming Help**
  6. lemon-tree

    lemon-tree x10 Minion Community Support

    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: May 20, 2010
  7. descalzo

    descalzo Grim Squeaker Community Support

    The file you grab with a XMLHttpRequest has to be on the same domain as the page where you are making the request.
  8. ....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: May 20, 2010
  9. descalzo

    descalzo Grim Squeaker Community Support

    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.
  10. absolutely descalzo...

    To answer your question tscrap
    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/

Share This Page