Handle Base64 and Binary File Content Types in Power Automate
Linn Zaw Win
3,405
Did you know that there are two types of file or image content type used in the input and output parameters? In this post, you will learn how to handle the different types of file content in the cloud flow action parameters.
When working with the input and output parameters for file content, you need to provide a value as Binary data type for most of the input parameters (e.g. Upload file or image content) and its counterpart action (e.g. Get file or image content) returns the value in Binary data type. However, some of the actions require the Base64 data type and some outputs return Base64 data type. So how do you identify what you need and when to convert which?
Understanding what type of file content to use in certain parameter is important. Sometimes, conversion of one type to another is required when the type of the output is different from the required input for the next step (e.g. get the Note attachment file and upload to SharePoint).
Identification
To identify the file content type of the input/output parameters, the Connectors Documentation is the best place to start.
Binary
All of the actions in the SharePoint connectors are consistent. Create file action requires the file content in Binary data type to upload the file and Get file content or Get file content using path returns the Binary data type. In Common Data Service (current environment) connector, Upload file or image content and Get file or image content uses the Binary data type but the rest of the parameters seems to be Base64.
Base64
To create a Note attachment, Base64 data type value needs to be populated in the Document column and Base64 value is returned when the documentbody column is retrieved from the Note table.
To use images in the Populate a Microsoft Word template action, the file content value needs to be constructed with the Compose action with Base64 value in the following format, unless the value is already a binary value of JPG or PNG.
{ "$content-type": "image/png", "$content": "iVBORw0KG...i/DhQmCC" }
You can read more details on this topic from Olena Grischenko's blog post about Populating Word template with the image field content from Microsoft Dataverse.
Conversion
To convert from Base64 to Binary data type, there is a base64ToBinary function. However, there is no such function for converting the other way round. The trick to converting the other way around is to set the Binary value into the Compose action and use the ['$content'] property of the output. You can also directly use the ['$content'] property of the Binary output too (e.g. outputs('Get_file_or_image_content')?['body/$content'] will return Base64 value). You can find more details about Binary to Base64 conversion in the blog post .
Now let's see the conversions in action.
Create Note Attachment from SharePoint File
Get file content action will return the SharePoint file content in Binary but creating a Note attachment requires Base64 value. Using the ['$content'] property of the output with the following expression will populate with Base64 value to the Document column.outputs('Get_file_content')?['body']?['$content']
Upload File with Binary Data
To upload the Binary data (e.g. documentbody column value of the Note attachment) to SharePoint or File data type column, base64ToBinary function can be used.base64ToBinary(outputs('Get_a_Note_Attachment')?['body/documentbody'])
Populate Word Template Image
To construct the file content for the image placeholder in the Word template with the Binary data type, ['$content'] property can be used as in the following expression.outputs('Get_file_or_image_content')?['body/$content']
Note
Compose action step is required to construct the input for the image content using the Base64 value and populate the output of the Compose step to the image placeholder. If the content JSON is directly populated, the step will fail with the following error.
Compose action step is required to construct the input for the image content using the Base64 value and populate the output of the Compose step to the image placeholder. If the content JSON is directly populated, the step will fail with the following error.
The image is not of type PNG or JPG. Please provide an image of type PNG or JPG.
Summary
There are two types of file content (Binary and Base64) used in the input and output parameters and it is important to use the correct type based on the required parameter. To convert from Base64 to Binary, base64ToBinary function can be used and ['$content'] property of the Binary output can be used when needing a Base64 value.
This was originally posted here.
*This post is locked for comments