Results 1 to 10 of 10

Thread: Can Javascript read a file to a string?

  1. #1
    tscrap is offline x10Hosting Member
    Join Date
    May 2010
    Location
    Alakode, Kannur, Kerala, INDIA
    Posts
    14

    Unhappy Can Javascript read a file to a string?

    //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. #2
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion
    Join Date
    Nov 2007
    Posts
    1,420

    Re: Can Javascript read a file to a string?

    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 by lemon-tree; 05-20-2010 at 11:21 AM.

  3. #3
    galaxyAbstractor's Avatar
    galaxyAbstractor is offline Community Advocate
    Join Date
    Oct 2007
    Location
    Land of Null and Insanity
    Posts
    5,503

    Re: Can Javascript read a file to a string?


  4. #4
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion
    Join Date
    Nov 2007
    Posts
    1,420

    Re: Can Javascript read a file to a string?

    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. #5
    vishal's Avatar
    vishal is offline -::-X10 Guru-::-
    Join Date
    Nov 2009
    Location
    INDIA
    Posts
    5,254

    Re: Can Javascript read a file to a string?

    **Moved To Programming Help**
    Regards ~ Vishal
    Giving Reputation (at bottom of my post ) is the best way to encourage the person who helped you on forums.

  6. #6
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion
    Join Date
    Nov 2007
    Posts
    1,420

    Re: Can Javascript read a file to a string?

    Using that code, you'll want something like this:
    HTML Code:
    <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 by lemon-tree; 05-20-2010 at 12:13 PM.

  7. #7
    descalzo's Avatar
    descalzo is offline Grim Squeaker
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    8,290

    Re: Can Javascript read a file to a string?

    The file you grab with a XMLHttpRequest has to be on the same domain as the page where you are making the request.
    Nothing is always absolutely so.

  8. #8
    thanks_but_no_thanks is offline x10Hosting Member
    Join Date
    May 2010
    Posts
    12

    Re: Can Javascript read a file to a string?

    ....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 by thanks_but_no_thanks; 05-20-2010 at 02:01 PM.

  9. #9
    descalzo's Avatar
    descalzo is offline Grim Squeaker
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    8,290

    Re: Can Javascript read a file to a string?

    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.
    Nothing is always absolutely so.

  10. #10
    thanks_but_no_thanks is offline x10Hosting Member
    Join Date
    May 2010
    Posts
    12

    Re: Can Javascript read a file to a string?

    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/

Similar Threads

  1. Can javascript read a file to a string or array?
    By tscrap in forum Free Hosting
    Replies: 4
    Last Post: 05-20-2010, 11:33 AM
  2. 550 Can't create directory: Read-only file system
    By buying in forum Free Hosting
    Replies: 6
    Last Post: 10-08-2009, 09:19 AM
  3. Replies: 0
    Last Post: 08-17-2008, 01:46 AM
  4. javascript and external javascript files problem
    By delon in forum Scripts, 3rd Party Apps, and Programming
    Replies: 6
    Last Post: 04-27-2008, 12:41 AM
  5. Read a txt file in java.
    By satheesh in forum Tutorials
    Replies: 0
    Last Post: 10-28-2007, 02:14 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
dedicated servers