Convert a Data URI to Base64 or 'File Contents' in Power Automate

October 7th 2021

We’ve had a number of support queries recently reporting errors processing requests which have stemmed from documents being passed as a Data URI as opposed to a base64 string or Power Automate ‘File Content’ property. To start lets clarify the differences between a Data URI and a base64 value.

  • Base64 is an encoding mechanism and is typically used to encode binary data (files)
  • A Data URI is a mechanism to enable files (typically images) to be included inline within HTML as opposed to a reference.

A Data URI actually includes a base64 string, consider the syntax below:

data:[<media type>][;base64],<data>

An example DataUri:

data:image/png;base64,iVBORw0KGgoAAAANS...

If you need to understand how to create a Data URI representation of a file to embed within HTML mark-up please review: Adding Images to HTML in Power Automate

Converting a Data URI to BASE64

If you already have a Data URI how do you convert this to its base64 representation or a Power Automate ‘File Content’ property? To convert to a base64 string use the following expression syntax:

base64(dataUriToBinary(<dataUriValue>))

The expressions execute two steps, firstly the Data URI is converted to a binary file and then the binary file is converted to a base64 string, see the outputs below:

Now, you may have observed from the run history that the expression is writing data into a string variable… it is very important to consider where you are sending data to! If the destination is a string variable then Power Automate will expect theĀ  base64() expression, however if you are trying to extract the file contained within a Data URI you should omit the base64() expression using:

dataUriToBinary(<dataUriValue>)

This will assure a correctly formatted File Contents value is passed:

We hope this post helps to clarify how to extract a base64 or a file from a Data URI within Microsoft Power Automate.

Author
Jay Goodison

Managing Director

You might also be interested in...