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

Native Development

Reply
Contributor
Posts: 45
Registered: ‎04-11-2012
My Device: Z10 Limited Edition
My Carrier: 00201003883386

How to encode files to Base64 to send them to remote PC

Hello all,

               I want to send bytes (images, files, mp3,...etc) to a remote PC with meta information. I think encoding to Base64 string will be a good solution for this case.

 

But I cant find API or example for that in Cascades.  Please advise 

When you are young, work to learn. Not to earn !
Contributor
Posts: 43
Registered: ‎01-05-2013
My Device: Dev Alpha
My Carrier: Telenor Hungary

Re: How to encode files to Base64 to send them to remote PC

Hi,

 

base64 encoding is available for QByteArray objects. You can load a file's content into QByteArray with QFile class. 

 

Loading is simple:

 

QFile file(QString("example.jpg"));
file.open(QIODevice::ReadOnly);

 

QByteArray loadedArray = new QByteArray(file.readAll());

 

When you have the QByteArray loaded, you can encode it to Base64 with these lines:

 

QByteArray* encodedFile = new QByteArray(loadedArray->toBase64());

 

cheers,

chriske

--------------------------------------------------------------
If my post was helpful or it was the solution for Your problem, please don't forget to give me a like, and mark my post as a solution. Thanks!
Developer
Posts: 150
Registered: ‎10-19-2012
My Device: Playbook, BB10DevAlphaB/C, Z10
My Carrier: Sonera, Saunalahti

Re: How to encode files to Base64 to send them to remote PC

If you like to do the base64 in pure QML you can do that too, see http://qt-project.org/doc/qt-4.8/qml-qt.html#btoa-method
Contributor
Posts: 45
Registered: ‎04-11-2012
My Device: Z10 Limited Edition
My Carrier: 00201003883386

Re: How to encode files to Base64 to send them to remote PC

Thanks for reply. but I am little confused.

this method returns byte array while I need Base64 String to send it to remote PC as a JSON string . Please advise
When you are young, work to learn. Not to earn !
Contributor
Posts: 45
Registered: ‎04-11-2012
My Device: Z10 Limited Edition
My Carrier: 00201003883386

Re: How to encode files to Base64 to send them to remote PC

Thanks for reply but this sample is QML from QT website while Cascades is a modified version of QT which doesnt support all QT functionality. Are you sure is sample is working fine with Cascades?

Any way I am searching for QT/C++ code sample because I will send image as a Base64 string to wrap it in a Json String that include image and some meta information
When you are young, work to learn. Not to earn !
Contributor
Posts: 43
Registered: ‎01-05-2013
My Device: Dev Alpha
My Carrier: Telenor Hungary

Re: How to encode files to Base64 to send them to remote PC

Don't worry, Cascades team just copied some parts of the Qt API reference... In Cascades documentation you can find instructions for Symbian and MeeGo too....

 

My example will work. Here is the Cascades reference:

http://developer.blackberry.com/cascades/reference/qfile.html

 

I'm sure.

--------------------------------------------------------------
If my post was helpful or it was the solution for Your problem, please don't forget to give me a like, and mark my post as a solution. Thanks!
Highlighted
Contributor
Posts: 43
Registered: ‎01-05-2013
My Device: Dev Alpha
My Carrier: Telenor Hungary

Re: How to encode files to Base64 to send them to remote PC

And you can convert QByteArray to QString.

QString newString = QString(myByteArray);
--------------------------------------------------------------
If my post was helpful or it was the solution for Your problem, please don't forget to give me a like, and mark my post as a solution. Thanks!
Contributor
Posts: 45
Registered: ‎04-11-2012
My Device: Z10 Limited Edition
My Carrier: 00201003883386

Re: How to encode files to Base64 to send them to remote PC

Thank you very much. will try then mark as a solution Smiley Happy
When you are young, work to learn. Not to earn !