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

Convert blob data to String ( and vice versa )

Yoshihiko.N
14-Alexandrite

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}

 

 

 

ACCEPTED SOLUTION

Accepted Solutions

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.

View solution in original post

8 REPLIES 8

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
14-Alexandrite
(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

 

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
14-Alexandrite
(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

 

Yoshihiko.N
14-Alexandrite
(To:Yoshihiko.N)

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.

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)

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.

Announcements


Top Tags