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);
8 Answers
-
Try using the
text
property instead:var doc = readContents.text;
See
filesystem.js
for more examples. -
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(); }
-
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?
-
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--------------------------
-
Same problem here. f.read().text returns null (on Android)
-
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?
-
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. -
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