none
Read external text file with javascript

    Question

  • I'm wondering if there's any way to read from an external text file with javascript.

     

    My gadget takes info from a txt-file and then creates a drop down menu in the gadget which uses that info to display webpages in the flyout.

     

    I get everything to work when I set my file location locally (c:\\x.txt), but it doesn't work when I set the location externally (http://www..../x.txt).

     

    I am somewhat new to ActiveX and Javascript, so I don't know all of it's limitations yet. Maybe someone can point me in the right direction to what is wrong. I appreciate all the help I can get.

     

    This is my coding for reading the text-file.

     

    var location = ...

    var fso = new ActiveXObject("Scripting.FileSystemObject");

    var tempFile = fso.OpenTextFile(location, 1);

    var line = tempFile.ReadLine();

     

    // and then I do some substringing and other stuff, but that's not where the problem is.

     

    So I guess my question is, how can I read a webhosted txt-file with javascript, if it is possible at all.

     

     

    Monday, April 16, 2007 1:52 AM

Answers

  • This code should help you read from a remote text file:

    Code Snippet
    var txtFile = new XMLHttpRequest();
    txtFile.open("GET", "http://my.remote.url/myremotefile.txt", true);
    txtFile.onreadystatechange = function() {
      if (txtFile.readyState === 4) {  // Makes sure the document is ready to parse.
        if (txtFile.status === 200) {  // Makes sure it's found the file.
          allText = txtFile.responseText;
          lines = txtFile.responseText.split("\n"); // Will separate each line into an array
        }
      }
    }
    txtFile.send(null);

     
    AFAIK, you can use a local file, just use the file:// protocol (ie file://C:/Users/Andy/Documents/textfile.txt).

    Andy

    Monday, April 16, 2007 8:59 AM

All replies

  • You're going to have to get a little creative.  FSO only works with local filesystem objects.  For a file on a web page, you can try retrieving it with XMLHttpRequest instead.  If you need to support both local and external files, you can try retrieving the local file with XMLHttpRequest (use file: notation, not sure if this will work when the file is not being served by a web server), or have separate codepaths for working with FSO or XHR.
    Monday, April 16, 2007 2:56 AM
  • This code should help you read from a remote text file:

    Code Snippet
    var txtFile = new XMLHttpRequest();
    txtFile.open("GET", "http://my.remote.url/myremotefile.txt", true);
    txtFile.onreadystatechange = function() {
      if (txtFile.readyState === 4) {  // Makes sure the document is ready to parse.
        if (txtFile.status === 200) {  // Makes sure it's found the file.
          allText = txtFile.responseText;
          lines = txtFile.responseText.split("\n"); // Will separate each line into an array
        }
      }
    }
    txtFile.send(null);

     
    AFAIK, you can use a local file, just use the file:// protocol (ie file://C:/Users/Andy/Documents/textfile.txt).

    Andy

    Monday, April 16, 2007 8:59 AM
  • Thank you guys, it really helped.
    Monday, April 16, 2007 1:39 PM
  • I have a .txt file that I use to store customer info. It is tab delimited. I want to know how to access this file from Javascript, parse the data, and store only the first "column" of info in a drop down list on my HTML page. The first "column" is the customer name. I don't need the rest of the information for the task I am trying to accomplish. The txt file will be local. Does any one know how to go about this?
    Thursday, May 14, 2009 4:14 PM
  • I have a .txt file that I use to store customer info. It is tab delimited. I want to know how to access this file from Javascript, parse the data, and store only the first "column" of info in a drop down list on my HTML page. The first "column" is the customer name. I don't need the rest of the information for the task I am trying to accomplish. The txt file will be local. Does any one know how to go about this?
    Assuming your txt file looks like this (imagine the ~ as tabs):

    customer 1~info1~info2~info3
    customer 2~info1~info2~info3

    .. and so on. Do something like:

    // str_line will be a line from the data in the second function
    // n will be the nth column. So if you want the 2nd column, put 2.
    function display_column(str_line, n) {
       document.write("<p>" + str_line.split('\t')[n - 1]);
    }
    
    // data will come from your XMLHttpRequest.responseText
    // we will split that with \n (or "enter key") as the delimiter)
    // and pass each line to the display_column() function above.
    
    function display_all(data) {
       var i, x = data.split('\n');
       for (i = 0; i < data.length; i++) {
          display_column(data[i], 1);
          // it's 1 because you only want to display the 1st column..
          // unless I misunderstood your question..
       }
    }
    DISCLAIMBER: THIS IS ALL THEORETICAL AND I HAVE NOT REALLY TESTED IT!
    Wednesday, February 03, 2010 11:04 AM
  • Hi Andy 

     

    Your code is working fine in IE. But it is not working in Firefox as well as in Chrome for me. Please help me.


    • Proposed as answer by Zia_MBSTU Friday, July 29, 2011 1:15 PM
    Friday, June 24, 2011 5:17 PM
  • I've not tested this, but there appears to be a solution on W3 schools at http://www.w3schools.com/XML/xml_parser.asp

    The code detects the browser type:

    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.open("GET","books.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML;

    Sunday, September 18, 2011 11:32 PM
  • Dear your code is not working on even getting data from local file . when i write that code and execute the code it download the file.
    Saturday, October 01, 2011 7:12 AM
  • JavaScript Works on Client Side. if You want Server Side, use PHP, ASP, JSP.

    Hope this helps

    Saturday, November 05, 2011 8:25 PM
  • You were close, but we were talking about text files, not XML.  Still, I adapted your post to serve for text files:

        

    var fileName = "http://www.yourdomain.com/file.txt";

    var txtFile; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari txtFile = new XMLHttpRequest(); } else {// code for IE6, IE5 txtFile = new ActiveXObject("Microsoft.XMLHTTP"); } txtFile.open("GET",fileName,false); txtFile.send(); var txtDoc=txtFile.responseText; var lines = txtDoc.split("\r\n"); // values in lines[0], lines[1]...



    • Proposed as answer by navyjax2 Tuesday, November 05, 2013 4:36 AM
    • Edited by navyjax2 Tuesday, November 05, 2013 4:37 AM
    Tuesday, November 05, 2013 4:35 AM