Pass binary data from backend to frontend

Hi everyone.
In my Vaadin app I have custom web component with both backend java class and corresponding typescript class. According workflow backend reads binary file and passing it to frontend component. So in Java class I have code like this:

byte[] bytes = Files.readAllBytes(%file path%);
getElement().executeJs("this.passBinaryFile($0)", bytes);

And implementation of method passBinaryFile in web component:

passBinaryFile(content: Uint8Array) {
        console.log('passBinaryFile called');
    }

So I got the following exception:

java.lang.IllegalArgumentException: Can't encode class [B to json

The same method works well for text files that passes as string. How could I pass binary data to frontend?

Wrap it as string :wink:

bytes are not really a thing data type… sounds similar to Support DTOs in executeJs (both parameters and return values) · Issue #18694 · vaadin/flow · GitHub

1 Like