How to get base64 of any file.

171 views
Skip to first unread message

ansari_abdullah

unread,
Feb 16, 2017, 9:19:59 PM2/16/17
to DroidScript
I am writing a program in Droid Script to get out base64 of any file
suppose [ var file = app.ReadFile('/file/location/'); ]
i have declared file location, now how should i get out the base64 of that file stored in *file* variable, please answer..!!
Thnx!

Steve Garman

unread,
Feb 17, 2017, 3:44:05 AM2/17/17
to DroidScript
Unfortunately, you have done much more than define the file location, you have attempted to read the contents of the file into a string variable.

That is fine if it is a text file but will cause problems if the file contains unpredictable binary data.

Base64 encoding of binary files in DroidScript is difficult. I have not yet done it reliably.

Can you explain what you are trying to achieve, please.

It may be much easier to offer you a simpler transmissible format, especially if the file needs to be recreated at the end of the process using DroidScript.

Steve Garman

unread,
Feb 17, 2017, 4:27:20 AM2/17/17
to androi...@googlegroups.com
If the other clients may not be written in DroidScript, you will probably need to find someone who has a DS plugin to base64 encode binary files.

If all the clients are using DroidScript, I would recommend bypassing Base64 and using app.CreateFile for both read and write.

The hex format available in app.CreateFile should be perfectly transmissible via the node server.


function OnStart()
{
 
// read contents of file
 
var fil = "/sdcard/Hello.png";
 
       
var file = app.CreateFile( fil, "r" );
       
var len = file.GetLength();
 
       
var data = file.ReadData( len,"hex" );
 
        file
.Close();
       
       
// display data
        alert
(data);
       
var data2 = data;
       
       
// write copy of file
       
var fil2 = "/sdcard/testout.png";
        app
.DeleteFile( fil2 );
       
var file2 = app.CreateFile( fil2, "rw" );
        file2
.Seek(0);
        file2
.WriteData(data2, "hex");
        file2
.Close();
}

Dave Smart

unread,
Apr 4, 2017, 5:42:42 AM4/4/17
to DroidScript
You can use the "base64" option with the app.ReadFile() method to get the binary data as base64 text which can be sent to online servers.
Reply all
Reply to author
Forward
0 new messages