How to Convert a Base64 Image to Binary and Save to SharePoint with Power Automate

Do you need to convert a base 64 string to binary in your Power Automate flow?

Converting a Base64 string to an image is required in order to save it to a SharePoint site. This can be especially useful when handling images transferred between systems as these are often in base 64 formats.

This article will show you the process to convert and store images from Base64 strings directly into SharePoint.

Understanding the Basics: Base64 vs Binary

Base64 and binary are two methods of representing data, each with distinct properties and uses.

Binary Data Representation

Binary is the most basic form of data storage and processing format. It’s a system that represents data in two-symbol form: 0 and 1.

These binary digits, or bits, are the smallest unit of data in computing. Binary is used directly by computers because of its efficiency and ease of implementation with digital electronic circuitry using logic gates.

Base64 Data Representation

Base64, on the other hand, is a binary-to-text encoding scheme. It’s primarily used when there’s a need to encode binary data, especially when that data needs to be stored and transferred over media designed to handle text.

Base64 ensures that the data remains intact without modification during transport.

In Base64, the binary data is represented as a string of characters from an available set of 64 different ones (hence the name “Base64”). This set usually includes A-Z, a-z, 0-9, +, / and = for padding.

Key Differences Between Base64 and Binary

  1. Purpose: Binary is used for direct data storage and processing in computers, while Base64 is used to encode this binary data into a format that’s safe for transport and storage in systems designed for text.
  2. Representation: Binary data is represented in bits (0s and 1s), while Base64 data is represented in a set of 64 different characters.
  3. Size: Encoding binary data to Base64 will result in it taking up about 33% more space.
  4. Compatibility: Binary data may not be safe or compatible with all systems, particularly those designed for text. Base64 encoding solves this problem by converting the binary data into a text format.
  5. Use Cases: Binary is used in almost all low-level and high-level computer operations. Base64 is commonly used when there’s a need to encode binary data, particularly when that data is to be sent over a protocol designed to handle text. This encoding helps to ensure that the data remains intact without modification during transport.

📝 Note: Base64 isn’t an encryption or obfuscation method for securing data, but a way to encode binary data for safe transmission or storage.

Base 64 Image Example

data:image/jpeg;base64,/9j/4AAQSkZJRgABAgEAlgCWAAD//gASTEVBRFRPT0xTIHYyMi4wAP/bAIQABQUFCAUIDAcHDAwJCQkMDQwMDAwNDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ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wCX4mv0HLMs+pfv69nWasktVBPdJ9ZPq9raLq349ev7X3IaRX4/8A2q+nOAKAI5VZ1KodpPf09fxrnrRnUg4UZ8knpzWvZX1aWmtr2vonqXFpO8lddgiiWFQi9B/kn8adGjHDwVGnflXd3bb1bbe7bu35ilJyfMzwP4z6hdWNxam3mkgQxtuKOUUndxkggZxnHeuyGGoV9cRSpza0TnFSaXlcyc5w0hJx9G0cD4B1i+u9ctI2uZpY/NXevmsw2/7QyeM+vFW8Hhaac6dClGS1TjCKafdNK6JVWpJ2lOTXZt2Pr6uc1IIIDBlQfk/hH931GfT/P14MPh3hnKEZXovWEHq6d780VK+sOsU9Y6q9rGs5c9m173V9/l3J67zIKACgDxz4+28dx4I1EyKGMQgkTP8Li5hAYe+CR9CRQB4z+yVbx7dXuNo80G0QN3CEXDFfoSAT64HpQB9k0AFABQB//Z

As an example, the above text is the base 64 representation of this small image.

{
  "$content-type": "image/jpeg",
  "$content": "/9j/4AAQSk...FABQB//Z"
}

Often, when you get base64 image content into Power Automate, it will be in the above format.

@{body('HTTP')?['$content']}

This means, if you want to access the base 64 text you will need to use an expression such as the above. Adding ['$content'] will return only this value from the JSON.

Binary Data in SharePoint

SharePoint stores images as binary data, not as Base64.

Images in SharePoint are stored within libraries, which are essentially folders for storing files.

When you upload an image to SharePoint, it’s stored as binary data in a database managed by SharePoint.

It’s important to note that while Base64 can be used to encode images for safe transmission over networks, it’s not typically used for long-term storage because it increases the total size of the file. For efficient storage and retrieval, SharePoint uses binary format to store images.

Converting Base64 String to Binary with the base64ToBinary Function

When working with base64 encoded strings, you may need to convert them into binary data to create an image in SharePoint using Power Automate.

This section will discuss the base64ToBinary function and how to use it for this purpose.

To start, you will need to get an image into your flow. This could be from many different sources, but this example will get an image from the web using an HTTP request.

  1. Add an HTTP action to your flow. Note, this is a premium action and will need a premium license to use.
  2. Select the GET option from the Method dropdown.
  3. Add the URI of your image. In this example the URL is https://www.powertechtips.com/wp-content/uploads/2023/09/PowerTechTipsLogo_50x50.jpg which is a jpeg image hosted on this site.
@{base64ToBinary(body('HTTP')?['$content'])}

Now you can use a Compose action to convert the results of the HTPP action from base 64 to binary.

  1. Add the Compose action to your flow and add the above expression.

The results of this expression can then be used directly as the Content in the Create file action to create a new image file in your SharePoint library of choice.

  1. Add the Create file action from the SharePoint connector to your flow.
  2. Select the Site Address of the SharePoint library where you want to save the image.
  3. Select the Folder Path where you want to save the image.
  4. Add a File Name and be sure to include the correct image file extension such as .png or .jpg.
  5. Add the dynamic content from the Compose action where you used the base64ToBinary function to convert the base 64 text to binary.

These steps will allow you to successfully convert a base64 encoded string into an image and save it to SharePoint.

Conclusions

This article showed you how to convert a base64 string to an image and save it to SharePoint.

Using the base64ToBinary function, you can easily convert base64 encoded strings into binary format.

Once the conversion is done, you can use the Create file action to save the binary image data to a SharePoint document library.

Have you tried saving base 64 images to SharePoint before? Let me know in the comments below!

About the Author

John MacDougall

John MacDougall

John is a Microsoft MVP and freelance consultant and trainer specializing in Excel, Power BI, Power Automate, Power Apps and SharePoint. You can find other interesting articles from John on his blog or YouTube channel.

Related Articles

Comments

0 Comments

Get the Latest Tech Tips

Write For Us

Are you a tech enthusiast with a talent for writing great content? Come write for us!

Follow Us

Follow us on social media to stay up to date with the latest in tech!