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

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

Convert blob data to String ( and vice versa )

Yoshihiko.N
13-Aquamarine

Convert blob data to String ( and vice versa )

Hi Developers,

 

I'd like to convert blob data to string and convert to string data to blob.

 

may I get your help to do the above ?

 

let blob_data = LoadBinary( { path : "/image.png" } );

//convert blob data to string to write it to SQL

let str_data = {something function for the conversion}

 

 

 

8 REPLIES 8
nmutter
14-Alexandrite
(To:Yoshihiko.N)

Hey,

normally you would use base64 functions to achieve this. Use Encode to convert to string and decode to return to binary. But I could not get it to work as I would like.

 

Maybe the ptc article https://www.ptc.com/en/support/article/CS335717 helps as some starting point.

They use the snippets available in Composer:

nmutter_0-1700055126011.png

 

Yoshihiko.N
13-Aquamarine
(To:nmutter)

Hi 
Thank you for your response.

I've created a test service to check if the resolution in the link you shared with me, but failed.

I expected to get original image but image data cannot be  restored.

 

I'd be happy someone help me on this.

 

YoshihikoN_0-1700084988001.png

 

nmutter
14-Alexandrite
(To:Yoshihiko.N)

Yes that's where I had the issue too. But you see in line 12 it changes your binary data and wraps {array:xxx} around it.

After decoding you would need to reverse this operation as well, to get only your xxx back. But I did not try it further for now

Yoshihiko.N
13-Aquamarine
(To:nmutter)

Hi 
Tahnk you for quick response.
I tried with the coding below but failed.

is there any miss there ?

 

YoshihikoN_0-1700174190604.png

 

Hi All,

I'd be happy if someone help me to know is there any miss in my code the above.

I think thingworx wants you to write it to a repository first.

Things["FileServerRepository"].SaveBinary({
path: "your path here") /* STRING */,
content: blobOriginal /* BLOB */
});

 

and from there you can do:

var result = Things["SystemRepository"].LoadImage({path: "your path here"})

 

That is what i did. and afterwards you van delete the image again.

nmutter
14-Alexandrite
(To:JU_10321794)

I also tried to get the code running. But the issue is that the LoadBinary returns an "object" of type "BlobPrimitive". But I am not able to create an instance of that type "Blob" after decoding the string back to binary. I just have a json-array which TWX does not interprete as BLOB. 

So what our service does is:

- LoadBinary -> Blob -> JsonArray -> Base64Encode -> String

- String -> Base64Decode -> JsonArray -x> Blob -> SaveBinary

But for the red part I do not have a solution...

 

TWX needs to have the blob-type for it to work. I did not find a way to create such an object in JavaScript code... No solution so far (Java Extension should work but this would be not so nice as workaround)

Rocko
17-Peridot
(To:nmutter)

The "trick" is SaveBinary accepts base64 encoded values for content as shown in https://www.ptc.com/en/support/article/CS212085.

Check here for an example.

Top Tags