FileUpload Component: Trying to get the byte string in base64 format

Good morning,

I'm attempting to get the base64 string of a file that I'm uploading using Ignition's FileUpload component, however there doesn't seem to be a list of string formats available to the event.file.getString() function in this page. Is there a list of formats handy, and if base64 isn't available for that function, is there a way I can encode the bytes to a base64 string? I've attempted the below code but get the following error "TypeError('b2a_base64() argument 1 must be bytes or Unicode, not com.inductiveautomation.perspective.gateway.script.PropertyTreeScriptWrapper$ArrayWrapper',)"

import base64

path =
bytes = event.file.getBytes()

#documentation says this returns UTF-8 format, not sure how to get to base64
defaultByteString = event.file.getString() 

base64String = base64.b64encode(bytes) #returns TypeError

Don't use jython's base64. Use java's Base64.

from java.util import Base64

def toB64(bytes):
	return Base64.getEncoder().encodeToString(bytes)

In your upload event:

b64str = someLibrary.toB64(event.file.getBytes())

That did the trick, thank you!

You're welcome.

Tip: any time you reach for the jython standard library's importable modules to solve some problem involving bytes, strings, sockets, web APIs, dates, times, and calendars--STOP. Look up the java way to do the corresponding task and call those java classes from jython.

Jython's python2 heritage means its stdlib doesn't separate bytes from strings properly, even though java does. And its python2.7 stdlib lacks many other updates and bugfixes that, while purely pythonic, only exist in python3. And the jython socket library has been an utter disaster, carrying through into anything that depends on it.


It was actually this thread that inspired me to try and rewrite my zip code from python libs to java libs, which you noticed and liked even though you weren't in that thread... I don't think I could follow as many threads on this forum as you do, @pturmel :slight_smile:

1 Like

I'm sure you could--this forum's underlying software (Discourse) makes it easy. I just set all categories to "watching", so the notification system keeps track of what I haven't seen. Then, whenever I take a break from coding (several times a day, typically), I just run through the topics that Discourse points out.

1 Like