Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X
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}
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:
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.
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
Hi
Tahnk you for quick response.
I tried with the coding below but failed.
is there any miss there ?
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.