Handle Base64 and Binary File Content Types in Power Automate

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.

However, Microsoft Docs will not cover everything and you will need to figure out some dynamic parameters like documentbody value of the Note table or image column value by running the flow first to see the output value that is available for you to use.

🔗 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.

The same applies to retrieving the image column which returns the Base64 value.

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.
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.

Comments

  1. This is so helpful! Thank you so much for putting all of the information together and clearing the confusion when working with file content in power automate.

    ReplyDelete
  2. Hi,
    We have been using this expression base64ToBinary() to create a note related case, when portal comment is added from dynamics portal.
    From yesterday the flow has started failing on the conversion step, due the mentioned flow error.

    Message for Error:
    {
    "error": {
    "code": "0x80040278",
    "message": "Invalid character in field 'documentbody': '.', hexadecimal value 0x00, is an invalid character.",
    "@Microsoft.PowerApps.CDS.ErrorDetails.ApiExceptionSourceKey": "Plugin/Microsoft.Crm.ObjectModel.AnnotationService",
    "@Microsoft.PowerApps.CDS.ErrorDetails.ApiStepKey": "753b8615-ecd8-db11-b397-0019b9204da9",
    "@Microsoft.PowerApps.CDS.ErrorDetails.ApiDepthKey": "1",
    "@Microsoft.PowerApps.CDS.ErrorDetails.ApiActivityIdKey": "5eacdd4f-8dbc-4add-9ec0-47ea1ef0da17",
    "@Microsoft.PowerApps.CDS.ErrorDetails.ApiPluginSolutionNameKey": "System",
    "@Microsoft.PowerApps.CDS.ErrorDetails.ApiStepSolutionNameKey": "System",
    "@Microsoft.PowerApps.CDS.ErrorDetails.ApiExceptionCategory": "ClientError",
    "@Microsoft.PowerApps.CDS.ErrorDetails.ApiExceptionMesageName": "InvalidCharactersInField",
    "@Microsoft.PowerApps.CDS.ErrorDetails.ApiExceptionHttpStatusCode": "400",
    "@Microsoft.PowerApps.CDS.HelpLink": "http://go.microsoft.com/fwlink/?LinkID=398563&error=Microsoft.Crm.CrmException%3a80040278&client=platform",
    "@Microsoft.PowerApps.CDS.InnerError.Message": "Invalid character in field 'documentbody': '.', hexadecimal value 0x00, is an invalid character."
    }
    }


    Any idea why this is happening? Nothing has been changed from our end.

    ReplyDelete
    Replies
    1. I am not sure how it was working previously but as mentioned in my example below, you will need to provide Base64 value to create a Note attachment. If you are using the expression base64ToBinary(), the input value becomes binary and thus, it is failing with that error.

      https://linnzawwin.blogspot.com/2021/02/handle-base64-and-binary-file-content.html#binary-to-base64

      Delete

Post a comment

Popular posts