Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Highlighted
Contributor
Posts: 20
Registered: ‎12-22-2012
My Device: PlayBook
My Carrier: Verizon
Accepted Solution

Convert binary image file (Blob) to base64 string on PlayBook?

I need to upload image files from the PlayBook to a webserver.

 

When using BB10, it's pretty easy, basically just using reader.readAsBinaryString(file) to get the file as a string, encode it into Base64, then send it with xml.send(fileData). That works fine with my Z10.

 

But the PlayBook seems to have it's own file handling system which doesn't work the same as BB10. I can read the image file just fine with blackberry.io.file.readFile(path, pbOpenImageFile), but that just gives me the binary Blob. How do I get that Blob converted to a Base64 string for sending with xml.send(fileData)?

 

blackberry.utils.blobToString(blobData) doesn't seem to work, at least not with my image data (works fine with my text files though). It also apparently has a size limit which could be a problem with images anyway.

 

Is there any way to get that Blob converted to a Base64 string?

Retired
Posts: 71
Registered: ‎09-19-2012
My Device: No sleep til BlackBerry 10
My Carrier: Bell

Re: Convert binary image file (Blob) to base64 string on PlayBook?

Checkout this base64 library for encoding and decoding base64 strings in legacy JavaScript environments.  Enjoy!  Let me know if you need any more assistance Smiley Happy

------------------------------
@grahamzibar
-_- zZZ
Contributor
Posts: 20
Registered: ‎12-22-2012
My Device: PlayBook
My Carrier: Verizon

Re: Convert binary image file (Blob) to base64 string on PlayBook?

[ Edited ]

If I'm reading that code correctly, I think it only *decodes* data ("at the moment just decodes a binary base64 encoded").

 

I've found similar code examples though, and none seem to do the same magic that reader.readAsBinaryString(imagefile); on the BB10 does. The string returned from that works fine for uploading (after encoding to Base64). But the PlayBook doesn't have reader.readAsBinaryString(file);... so I guess I'm looking for it's equivilent, if there is one?

Retired
Posts: 71
Registered: ‎09-19-2012
My Device: No sleep til BlackBerry 10
My Carrier: Bell

Re: Convert binary image file (Blob) to base64 string on PlayBook?

Oh... I've used this library before and I could have sworn it had both (but perhaps I just used the decoding part).

 

So Sorry!!!  Anyway, here's another batch of code someone wrote that apparently does both decoding AND encoding Smiley Happy

 

http://ntt.cc/2008/01/19/base64-encoder-decoder-with-javascript.html

 

looks legit!

------------------------------
@grahamzibar
-_- zZZ
Contributor
Posts: 20
Registered: ‎12-22-2012
My Device: PlayBook
My Carrier: Verizon

Re: Convert binary image file (Blob) to base64 string on PlayBook?

Got it.

 

Taking the idea from here something like:

var can = document.getElementById('canvas');
var ctx = can.getContext('2d');
var img = document.getElementById('tweetpic');
ctx.drawImage(img, 0, 0);
var b64Text = can.toDataURL();
b64Text = b64Text.replace('data:image/png;base64,','');
var fileData = b64Text;

And then xml.send(fileData); as usual Smiley Happy