Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Reading a text file

I would like to read a text file the I have downloaded via XHR. I have already verified that it exists, but when I try and read the file to just output to the console, I get [object TiBlob]. How do I get the content as a String?

var readContents;
var readFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'data.txt');        

if (readFile.exists()) {
     readContents = readFile.read();
     Ti.API.info('File Exists');  
}

var doc = readContents.toString();
Ti.API.info('Contents = ' + doc);
— asked March 23rd 2010 by Jeff Pierdomenico
  • file
  • filesystem
  • filesystem.getfile
  • iphone
0 Comments

8 Answers

  • Try using the text property instead:

    var doc = readContents.text;
    

    See filesystem.js for more examples.

    — answered March 23rd 2010 by James K
    permalink
    1 Comment
    • This worked for me; why isn't this documented?

      — commented February 25th 2011 by Nathan Weir
  • Friends it worked for me in Android emulator. Simple. Yet to check with iPhone

    To create a text file and write in it i did

    var myFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, 'myFile.txt');
            myFile.write('Good job');
    

    Then to open it and read i used

            var openMyFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, 'myFile.txt');
            if(openMyFile.exists())
            {
                //textFieldLast is some textfield already declared on screen
                textFieldLast.value  = openMyFile.read().toString();
            }
    
    — answered November 30th 2011 by Abisek Sounder Raj
    permalink
    0 Comments
  • I have the exact same problem, toString() returns a tiblob and text returns null, but if use a webview to display the file it shows the correct content… any suggestions?

    — answered March 23rd 2010 by JT Jankowiak
    permalink
    0 Comments
  • I had similar problems and managed to get this to work for me, probably not best way but…

    
    var xhr = Titanium.Network.createHTTPClient();
    xhr.open("GET","http://www.yourdomain.com/test.txt");
    xhr.onload = function(){
      // check status of connection to server
    statusCode = xhr.status;
        //check the response code returned
        if(statusCode == 200)
        {
            var doc = this.responseText;
    //write data from downloaded text file to local text file
            var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'test.txt');
            f.write(doc);
        }
    
        };
    xhr.send();
    //-----------------------------------------------------------------------
    //---------------------local file read---------------------------------------
    var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, 'test.txt');
    
    // read the file into contents var
    var contents = f.read();
    
    // print out contents of file 
    //Ti.API.info('contents = ' + contents.text);
    
    //-----------------------------------------------------------------------
    //--------------------------end of file read--------------------------
    
    — answered March 28th 2010 by Robert Greenock
    permalink
    1 Comment
    • thanks it worked for me!

      — commented May 22nd 2012 by Martin Guillon
  • Same problem here. f.read().text returns null (on Android)

    — answered March 29th 2010 by Stefan Zollinger
    permalink
    0 Comments
  • Using the codes discussed to read a text file is working fine. However, the codes do not work for reading a unicode text file. What kind of changes are required for the codes shown in order to read a unicode text file?

    — answered March 23rd 2012 by Kim Hock Ng
    permalink
    0 Comments
  • Anybody figured this one out?
    I'm writing a webserver in Titanium for an iPhone app.
    Returning plain text responses works.
    Returning images requires me to read the file… Which doesn't work.
    Having the same problem as the topic starter.

    — answered December 10th 2010 by Ronny Welter
    permalink
    0 Comments
  • I just find something using informations from James K using filesystem.js (link update, the last one for 1.0.x version, but now it's 1.6.0)

    So instead of

    readContents = readFile.read();//return as blob format
    

    we just have to add .text then:

    readContents = readFile.read().text;//return as text format
    
    — answered March 25th 2011 by Florent Cardot
    permalink
    4 Comments
    • Doesnt work for me. returns variable as null for android. works on iPhone emulator but not the iPhone device.

      — commented July 28th 2011 by Corey Hartley
    • Same problem here (Android). Can't test it on iPhone.

      var xmltext = file.read();
      Ti.API.info(xmltext);  // returns my xml
      var txt = xmltext.text;
      Ti.API.info(txt);   //returns my xml
      var doc = Ti.XML.parseString(txt); //ERROR: Message: TypeError: Cannot call method "parseString" of undefined  - Source: null
      

      anyone found a solution?
      SDK 1.7.2

      — commented August 19th 2011 by Michael G.
    • This worked on previous versions but not that I can make happen on 1.7.2. I get a —– Titanium Javascript Runtime Error —–, in the logs at any reference to *.text. Another 2 hours down the damn drain. Docs seem to be wrong, examples are wrong …

      — commented October 12th 2011 by Hal Burgiss
    • This worked on previous versions but not that I can make happen on 1.7.2. I get a —– Titanium Javascript Runtime Error —–, in the logs at any reference to *.text. Another 2 hours down the damn drain. Docs seem to be wrong, examples are wrong …

      — commented October 12th 2011 by Hal Burgiss
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.