Titanium Community Questions & Answer Archive

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

How to get a Mobile TiBlob as string

I am using Titanium Developer 1.2.1, iPhone sdk 4.1, Android 1.6 sdk, Titanium sdk 1.4.1.1.

I need to be able to see the bytes in a TiBlob as a string. I'm using the mobile version of appcelerator.

I have tried this but it doesn't work inside the success function of openMediaGallery:

var image = event.media;
var tempFile = Titanium.Filesystem.createTempFile();
tempFile.write(image);
var contents = tempFile.read();

Ti.API.log( 'contents: ' + contents.toString() ); // outputs "[object TiBlob]"
Ti.API.log( 'contents: ' + contents ); // outputs "[object TiBlob]"
Ti.API.log( 'contents: ' + JSON.stringify(contents) );
Ti.API.log( 'contents: ' + contents.text ) // outputs "null"

Is there any way to get the contents of a TiBlob as a string?

— asked October 5th 2010 by Evan Dontje
  • blob
  • file
  • mobile
  • tiblob
  • tostring
0 Comments

6 Answers

  • Apparently TiBlob is very sensitive to the contents…..if there is anything that is not ASCII, then it returns null for .text

    — answered January 10th 2011 by Kouroche Sedaghatian
    permalink
    0 Comments
  • I'm was using 1.7.2 and this was not working, so I downloaded the bleeding edge (1.7.5) and tried, still no luck. I opened up TiBlob.m and TiBlob.h to inspect, I added a function and got it to work

    add this to TiBlob.m
    -(NSString)textRep
    {
    NSData
    theData = [NSData dataWithContentsOfFile:path];

    //NSData* pictureData = UIImagePNGRepresentation(image);
    NSString* pictureDataString = [theData base64Encoding];
    return pictureDataString;
    

    }

    and this to TiBlob.h
    @property(nonatomic,readonly) NSString* textRep;

    now, clean and rebuild the project

    you should now get a base64 encoded string when you access yourBlob.textRep

    — answered November 2nd 2011 by Dereck D
    permalink
    7 Comments
    • TiBlob.m

      -(NSString*)textRep
      {
          NSData *theData = [NSData dataWithContentsOfFile:path]; 
      
          //NSData* pictureData = UIImagePNGRepresentation(image);
          NSString* pictureDataString = [theData base64Encoding];
          return pictureDataString;
      }
      

      TiBlob.h

      @property(nonatomic,readonly) NSString* textRep;
      

      — commented November 2nd 2011 by Dereck D
    • This is amazing. Thank you. After hours of efforts I finally managed to get this working. Brilliant.

      — commented November 28th 2011 by Anuj Surana
    • Thanks a million!!! The other module that I tried (com.clearlyinnovative.utils) doesnt work, and this one works perfectly!!
      And I am impressed with how concise it is.

      — commented December 16th 2011 by Ganna Kozynenko
    • Hi Dereck D

      I am in trouble for a long long time by upload a image by byte.
      would you tell me where is the TiBlob.m / TiBlob.h. I'm using sdk 1.8

      — commented February 24th 2012 by j h
    • Problem here. It works like a charm, but Apple complains of non-public API when I am submitting it. Any ideas?

      — commented February 24th 2012 by Ganna Kozynenko
    • Problem solved. This is no longer necessary, it is possible to use Ti.Utils.base64encode, but with one catch - I had to remove \n from the resulting string, or else the server didnt like it (literally, like this: myEncodedString.replace(/\n/g,""))

      — commented February 24th 2012 by Ganna Kozynenko
    • Ti.Utils.base64encode works with blobs in 1.8.1 and maybe in 1.8.0

      — commented February 24th 2012 by Ganna Kozynenko
  • I was looking at this the past weekend, so I could use some existing APIs on my server that wanted an image as an array of bytes. Basically I came to the conclusion that you can't with the current Appcelerator Mobile SDK. You can on the Desktop apparently, but I ended up putting a new endpoint on my server and passing there Ti.Blob using XHR.

    Maybe 1.5 will bring this feature? There is some cool stuff in the commit logs, including fixes for animations on Android. I drool I tell you! Drool!

    (Though it did not really help me, you can save the stuff out as a temporary file and try to play with that. There is no file API to read data as chunks of bytes though, so making a byte array from that also turned out to be a dead end for me.)

    — answered October 5th 2010 by John Storey
    permalink
    2 Comments
    • you should use the text property of Titanium.Blob. see:
      http://developer.appcelerator.com/question/33111/object-tiblob-response-of-tostring

      — commented December 22nd 2010 by jony kalavera
    • The link you reference comes back with a null everytime I use someblob.text

      — commented December 29th 2010 by Will Dent
  • tempFile.read().text
    

    is not working for me, The File Im trying to read is of 30,708 bytes and containing text data

    — answered November 27th 2012 by ankur garha
    permalink
    0 Comments
  • This worked for me

    tempFile.read().text;
    
    — answered July 12th 2011 by Tuna K
    permalink
    1 Comment
    • Did you tried this?

          var image=imageView.toImage();
          var str = Ti.Utils.base64encode(image);
          alert(str.length);
      
          var image2=Ti.Utils.base64decode(str);
          imageview2.image=image2;
      

      It is working fine at my end.

      Cheers

      — commented December 11th 2012 by Ali Akram
  • .text property worked for me

    — answered March 27th 2011 by David
    permalink
    0 Comments
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.