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

base64 to blob via base64DecodeBytes gives error

oskarberntorp
12-Amethyst

base64 to blob via base64DecodeBytes gives error

Hi,

I have a service that I am utilizing to save files, I have a remote thing written in C# calling it.

 

Among the fileName and type, I also have the fileData as a parameter of type string, this string is a base64Encdoded blob (byte array).

When I run the base64Encoded byte array througth base64DecodeBytes, I get this error: 

 JavaException: java.lang.Exception: Unable To Convert From org.json.JSONObject to BLOB

 

Basically, I am doing something wrong, how do I decode a base64 string into it's underlying byte array so it can be saved with saveBinary? What I think is odd is that the error talkes about a json object, when the FileData parameter is a text type and is a base64 string,

 

When trying to send the byte array as a string in the json request and sending the file into the saveBinare function, I get this error: JavaException: java.lang.Exception: Invalid length

Kindly

Oskar Berntorp

1 ACCEPTED SOLUTION

Accepted Solutions

I think for PDFs base64 it not the best option, as PDFs are rather large objects and base64 is not an efficient coding. I'd rather try to use a protocol supporting binary data to transmit PDFs for efficiency.

In your case, I wonder where the problem is, if it is base64 and you can decode it, it shouldn't matter if it is an image or a PDF or any other binary format. Here's some sample code to encode and restore:

 

Encode to base64:

 

function encodeBase64(repo, path) {
    let s = repo.LoadBinary({path:path});
    var blobArray = [];
    logger.warn("s.length="+s.length);
	for (var i = 0; i < s.length; i++)
        blobArray.push(s[i]);
    
    b64=base64EncodeBytes({"array":blobArray});
    repo.SaveText({	path: path+".b64", content: b64});
    return b64;
}

result=encodeBase64(Things["SystemRepository"],"thumbnail.png");

 

 

Restore binary from b64 String:

 

function restoreBase64(repo, path) {
    let b64 = repo.LoadText({path:path});
    repo.SaveBinary({ path: "decoded-"+path.slice(0,-4), content:b64});
    return b64;
}

result=restoreBase64(Things["SystemRepository"],"thumbnail.png.b64");

 

 

if it's PNG or PDF shouldn't matter.

 

View solution in original post

3 REPLIES 3

Have you checked the base64 string is valid by pasting it into a different converter? Does it contain newlines?

Invalid length would indicate it didn't receive valid base64,

 

base64EncodeBytes takes a json parameter with field called array you will have to populate:


// base64EncodeBytes(value:JSON):STRING
let encodedValue = base64EncodeBytes(/* SampleInput={"array":[arrayOfBytes]} */);

Hi @Rocko ,

I have now tested again, the base64 string could be decoded but then it does not seem to be correctly translated inte a pdf. Whet testing with base64 guru I had to use their tool base64 to pdf, it would be good if thingworx had something similar. Images can be converted from a base64 representation, why not pdfs?

Kindly

Oskar Berntorp

I think for PDFs base64 it not the best option, as PDFs are rather large objects and base64 is not an efficient coding. I'd rather try to use a protocol supporting binary data to transmit PDFs for efficiency.

In your case, I wonder where the problem is, if it is base64 and you can decode it, it shouldn't matter if it is an image or a PDF or any other binary format. Here's some sample code to encode and restore:

 

Encode to base64:

 

function encodeBase64(repo, path) {
    let s = repo.LoadBinary({path:path});
    var blobArray = [];
    logger.warn("s.length="+s.length);
	for (var i = 0; i < s.length; i++)
        blobArray.push(s[i]);
    
    b64=base64EncodeBytes({"array":blobArray});
    repo.SaveText({	path: path+".b64", content: b64});
    return b64;
}

result=encodeBase64(Things["SystemRepository"],"thumbnail.png");

 

 

Restore binary from b64 String:

 

function restoreBase64(repo, path) {
    let b64 = repo.LoadText({path:path});
    repo.SaveBinary({ path: "decoded-"+path.slice(0,-4), content:b64});
    return b64;
}

result=restoreBase64(Things["SystemRepository"],"thumbnail.png.b64");

 

 

if it's PNG or PDF shouldn't matter.

 

Top Tags