How to add a file to a Power Automate variable

November 25th 2021
Back to all blogs

It’s a fairly common requirement to store a file within a Power Automate variable and typically this occurs where you have conditional logic which will transform a file. Consider the following flow:

This flow doesn’t work! A simple fix would be to duplicate the ‘Create file’ action within each branch of the condition however, whilst this would work for this example, there are many other  scenarios where this would not work or would create significant and unnecessary complexity.

So how do we fix this flow without duplicating the SharePoint ‘Create file’ action?

Power Automate Variables and Files

The fix is very simple; we’ll add a variable to the flow and add the file generated within each branch of the condition to the variable before passing the variable to the SharePoint ‘Create file’ action:

We can then execute the flow and it will execute without any errors:

However, if you try to open the file added to SharePoint (or whatever you have done with the file) you will notice that the file is corrupt!

This is caused by the formatting of the data added to the variable and we need to add additional control to the flow to ensure that data is passed in the correct format. We recommend the following rules:

  1. Always store just the base64 string representation of a file within a Power Automate string variable
  2. When passing the string variable to another action’s file content property, wrap the variable in the base64ToBinary() expression

Let’s explore this in some more detail…

Reviewing the run history of the previous failed flow you can instantly see the issue; the file was added to the string variable as a binary representation which caused the onward error:

We need to adhere to our first rule: Always store just the base64 string representation of a file within a Power Automate string variable

This is a little tricky because different actions handle files in different ways. Some actions (like Encodian) pass just a base64 string whilst some actions (typically Microsoft) pass a JSON structure containing the mime type and the base64 string… a little complicated, but just adhere to these rules and you’ll be fine!

Check the providing action’s output value, you can see this action is providing base64 only:

To add the base64 value to a string variable correctly we need to use the string() expression:

This is an example of an action providing the file as a mime type and a base64 string:

To add the base64 value only to the variable correctly we need to use the base64() expression:

Now, if you are dealing with actions which only ever provide a file content property as a mime type and a base64 value, then you can simply create an ‘object’ variable and then pass the value directly and it will just work (in most scenarios). Additionally, for actions providing just base64 you could use an object variable and wrap the ‘File Content’ in a base64ToBinary() expression to add the file to the object variable, and then use the base64() expression to pass the variable to another action… My point being, this isn’t the only way to store files in variables, and this post outlines the rules we typically align to which cover most scenarios.

At this stage your variable should contain only a base64 string representing the file.

We now need to update the flow to adhere to our final rule: When passing the string variable to another action’s file content property wrap the variable in the base64ToBinary() expression

This is really simple, see below:

Our flow will now be working and the file produced will not be corrupt!

Hopefully this post has provided some guidance to an area within Flow which can be quite tricky to master. If you would like to discover more please also review the following video covering this topic:

Author
Jay Goodison

Managing Director

Back to all blogs

You might also be interested in...