In this article, we'll explore the process of converting attachment files into Base64 strings within Power Apps and subsequently sending them as email attachments through Power Automate. This workflow enables efficient handling of attachments within your Power Platform solutions.
In Power Apps, while there isn't a direct function to convert attachment files to Base64 string, we will see how we can achieve that functionality for multiple files.
So, in our Power App, we've got a basic attachment control where users can upload files and a button that they can click to kick off a Power Automate flow.
To convert the attached file to base64, we will create a separate power apps component.
- Go to the Tree view tab click on component and click on new Component (Give component a name).
- On the right side component panel click on New Custom Property.
- On edit custom property panel. Give your property name and select Property Type as Action and Return type data as text as shown below.
- Click on the new parameter. Name the parameter and select the data type as Text.
- Then click on Create.
Now click on your custom property and add the following code.
Set(varImageValue,FileData);
Mid(JSON(Image1.Image,JSONFormat.IncludeBinaryData),Find("\",JSON(Image1.Image,JSONFormat.IncludeBinaryData))+2,Len(JSON(Image1.Image,JSONFormat.IncludeBinaryData))-Find("\",JSON(Image1.Image,JSONFormat.IncludeBinaryData))-2)
The formula provided eliminates redundant quotation marks from a base64 string.
Add an image control within the component and configure its image property to be the varImageValue variable. With this setup, your components are all set for use.
Now, add your component to the screen.
Write the code below in the attachment controls onAddFile and onRemoveFile events.
ClearCollect(attachments,[]);
ForAll(
AttachmentsControl.Attachments As doc,
Collect(
attachments,
{
Name: doc.Name,
ContentBytes: Convert_1.ToBase64(doc.Value)
}
)
);
Now, we'll Create a Power Automate flow to send the attachment via email.
Click on power Automate and create a new Flow and text input parameter.
Parse the attachment data and create a variable type as an array.
Then create the attachment content to send in the email.
$content-type : substring(items('Apply_to_each')['ContentBytes'],5,add(indexOf(items('Apply_to_each')['ContentBytes'],';'),-5))
$content: substring(items('Apply_to_each')['ContentBytes'],add(nthIndexOf(items('Apply_to_each')['ContentBytes'],',',1),1))
And finally, attach the Attachment Content variable in the attachments section of Send an email V2.
In power apps, on submit, click call flow.
In summary, we successfully converted attachment files into Base64 strings within Power Apps and sent them as email attachments using Power Automate.
We first developed a custom component in Power Apps to manage the conversion process, allowing us to handle file data effectively.
This component was seamlessly integrated into our Power App, providing users with a straightforward way to upload files via an attachment control.
We then implemented a Power Automate flow to handle the parsed attachment data, preparing it for email transmission. This involved setting up variables and specifying the appropriate content type.
Finally, we configured the flow to send an email with the converted attachment content, thereby completing the entire workflow.