Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com
I was recently conducting a training on Power Automate and there in came this question. How do I convert a byte array to base64 string in PowerApps.
After all there is a function in Power Automate – base64ToBinary which converts a base64 string to Binary. But there is no function like binaryToBase64 in Power Automate. If you are from C#, you would be thinking wish you had a method similar to Convert.ToBase64String in Power Automate too.
Well, I can understand the disappointment here. But frankly speaking the method is never needed. And it is because of that way Power Automate handles an incoming byte array.
For example you are reading a file from one drive and want to convert the binary file into Base64 string for further processing. You actually don’t need to. This is because when Power Automate will handle the incoming bytes, it will automatically convert the incoming bytes as Base64 with content type as “application/octet-stream”.
“$content-type”: “application/octet-stream”
And to get the content in Base64 format, you just need this expression – triggerBody()?[‘$content’]
Let’s see this with an example here.
I create a Manual trigger flow with “abcd” as my initial input parameter. Below is my flow definition
Nothing complex about the flow. Steps are explained below
- Convert the text into base64 string using expression – base64()
- Use the Compose operation to convert the base64 encoded string into binary using the base64ToBinary() expression.
- In the final step I assign the Binary input to a string variable
Let’s see what is the final output when we start with initial input – “abcd”.
Observe the Compose step where I used base64ToBinary() expression. As you can see, that the content is actually still in base64 format but the content-type has been changed to “application/octet-stream”.
In the final step when I am using the Output from from Compose Action and assigning it to a string variable, it is actually decoding the base64 text into plain text.
And that is why there is no function like binaryToBase64. After all the content is always in base64. The content-type reflects the type of content stored internally.
Hope this helps!
Debajit Dutta
(Microsoft MVP)
Discover more from Debajit's Power Apps & Dynamics 365 Blog
Subscribe to get the latest posts sent to your email.