Titanium Community Questions & Answer Archive

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

correct way to encode an image?

Can anyone please help with the correct way to encode an image so that I can send it via web services to my content management system?

The following should work but the encoded file doesn't decode back into the original so it can't be viewed as an image any longer.

var f = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory,'KS_nav_phone.png');
var blob = f.read();
var encoded = Ti.Utils.base64encode(blob.toString());

Is this a bug in converting blobs to strings then encoding to base64 or am I'm missing something totally obvious here?

Thanks in advance!

Rob :)

— asked November 1st 2010 by Robert Nagy
  • base64encode
  • blob
  • filesystem
  • getfile
  • read
  • tostring
  • utils

2 Answers

  • This isn't an answer but it is an explanation. blob.toString() returns this text [object TiBlob] which base64 encodes to a non-image. If I find an answer, I'll let you know.

    — answered November 1st 2010 by John McKnight
    • Thank you John for shedding light on this. It appears others are also having trouble uploading images to a server, for example: Alexander's question.

      Is there any way to access the blob as a string?

      I can upload text files successfully using the above code but not images.

      — commented November 2nd 2010 by Robert Nagy
    • No. It does not appear that you can access a blob as a string. The code I have looked at in the iOS implementation seems to show it isn't an option and I tried several combinations with no luck. I do know you can post through HTTPClient you can send the blob directly to your web service. I haven't done it with an image but I have successfully posted audio to PHP on a server.

      — commented November 2nd 2010 by John McKnight
  • When sending images, I just let the http client encode it the image for me - just like the kitchen sink example. They only thing I had to change from the example was setting the content-type:

    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    — answered December 17th 2011 by Jeff Bonnes
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.