cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X

How do I encode an file retrieved from the SystemRepository.LoadBytes() function into base64 ?

zgross1
3-Visitor

How do I encode an file retrieved from the SystemRepository.LoadBytes() function into base64 ?

Hello,

 

I am attempting to encode a byte array (blob) into a base64 string so that I can save it, then send it via Rest call to an external service from thingworks.  Below is the current version of my code:

 

var blob = Things["SystemRepository"].LoadImage({
    path: filename /* STRING */
});

var jsonObj = { //an attempt to fix the "no array member found in the JSON object error," to no avail.
"bytes":[]
};

for(var i = 0; i < blob.length; i++)
{
    jsonObj["bytes"][i] = blob[i]; //copy the array to the jsonObj array
}

var arr = jsonObj["bytes"];

var basestring = base64EncodeBytes(jsonObj); 

 

The current issue I am having is when I try to convert to base64 string. I am receiving an error " Invalid Argument Type (no array member found in the JSON object" Prior to this, i received an error when passing the array, rather than a JSON object with an array, straight into the function and received the error "Invalid Argument Type (not a JSON object)". Prior to that, I had passed the result from the LoadBytes function straight into the function, and received the same error "not a json object."

 

The filename given to the LoadBytes service is valid.

 

I feel that I am close, yet I can not figure out how to get the image to base64 string. Any help is appreciated, thanks!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

Hello,

 

Indeed, you were very close -- needed just to change "bytes" to "array" :)

 

Here's the short working version:

var blobArray = [];
for (var i = 0; i < blob.length; i++) blobArray.push(blob[i]);
var result = base64EncodeBytes({ array: blobArray });

Regards,
Constantine

View solution in original post

2 REPLIES 2

Hello,

 

Indeed, you were very close -- needed just to change "bytes" to "array" :)

 

Here's the short working version:

var blobArray = [];
for (var i = 0; i < blob.length; i++) blobArray.push(blob[i]);
var result = base64EncodeBytes({ array: blobArray });

Regards,
Constantine

I was indeed very close! Thank you very much :)

Top Tags