Start a new topic
Answered

Convert base64 file content from json field to physical file

Hello, I am reading a JSON file that contains information which I then save into a database. One piece of this information is a string representing the content of a file, encoded in Base64.


I would like to convert this Base64 content back into a file on my computer, but I haven't been able to figure out how to do it!


Best Answer

Hello,

The documented approach is a scripting action in a process, using Java’s built-in Base64 decoder.

  1. Add a Java Native Scripting action with Scripting Language set to groovy. Put the script in the action’s expression editor, without the %e(...)% wrapper.
  2. Get the Base64 string into the script. For one file per row, link a bind link from your query to the action and read the column with __ctx__.getSourceResultSet().getString("<column>"). For a single file, store the string in a session variable and read it with __ctx__.getVariableValue("<path>").
  3. Decode it and write the bytes:
groovy
importjava.util.Base64
importjava.nio.file.Files
importjava.nio.file.Paths
def rs = __ctx__.getSourceResultSet()
def bytes = Base64.getMimeDecoder().decode(rs.getString("FILE_CONTENT"))
Files.write(Paths.get("C:/temp/output", rs.getString("FILE_NAME")), bytes)


Things to check if the file is missing or corrupted:

  • The file is written on the machine running the runtime. With Designer’s local runtime that is your computer; with a remote runtime it is the server. The folder must exist and be writable.
  • If the value starts with data:...;base64,, strip that prefix first.
  • For URL-safe Base64 (- and _), use Base64.getUrlDecoder().

Docs: Scripting in processes, Scripting context, Java Native Scripting action.

Thank you

1 Comment

Answer

Hello,

The documented approach is a scripting action in a process, using Java’s built-in Base64 decoder.

  1. Add a Java Native Scripting action with Scripting Language set to groovy. Put the script in the action’s expression editor, without the %e(...)% wrapper.
  2. Get the Base64 string into the script. For one file per row, link a bind link from your query to the action and read the column with __ctx__.getSourceResultSet().getString("<column>"). For a single file, store the string in a session variable and read it with __ctx__.getVariableValue("<path>").
  3. Decode it and write the bytes:
groovy
importjava.util.Base64
importjava.nio.file.Files
importjava.nio.file.Paths
def rs = __ctx__.getSourceResultSet()
def bytes = Base64.getMimeDecoder().decode(rs.getString("FILE_CONTENT"))
Files.write(Paths.get("C:/temp/output", rs.getString("FILE_NAME")), bytes)


Things to check if the file is missing or corrupted:

  • The file is written on the machine running the runtime. With Designer’s local runtime that is your computer; with a remote runtime it is the server. The folder must exist and be writable.
  • If the value starts with data:...;base64,, strip that prefix first.
  • For URL-safe Base64 (- and _), use Base64.getUrlDecoder().

Docs: Scripting in processes, Scripting context, Java Native Scripting action.

Thank you

Login to post a comment