Hello everyone and welcome to my blog. In today’s blog I will show how you can upload multiple attachment from your Power App using the file attachment control
If you are not aware on how to use an attachment control outside the form, you can read my blog.
Let’s get back to the requirement. I have an attachment control on the form. I will use the attachment control to upload multiple attachments as notes in DataVerse.
Remember, the attachment control is outside of a form. So we need to write some custom formula to achieve this requirement. Let’s see how we can do it.
To accomplish the requirement, I insert a Gallery control. We are going to hide it when we are done with the configuration. Inside the Gallery, delete all other controls and only keep the following controls.
- Label control
- Add Picture control
I have renamed the controls accordingly. Check for the screenshot below.
Next, I set the Items property of the Gallery to the Attachments property of the Attachments control.
The label control will display the file name and the Image control inside the Add Picture control will be set the to the uploaded file content. I set the properties of the control accordingly.
Set the label Text property
Set the Image property of the Image control inside the Add Picture control
We are at the final step now. Now it’s time to explore the formula on Submit button click. Below is the formula
ForAll(
Gallery4.AllItems,
Patch(
Notes,
Defaults(Notes),
With(
{
FileContent: Substitute(JSON(
FileContentImage.Image,
JSONFormat.IncludeBinaryData
), """","")
},
{
Title: FileName.Text,
'File Name': FileName.Text,
'Is Document': true,
Document: Mid(
FileContent,
Find(
"base64",
FileContent
) + 7
)
}
)
)
)
The formula may seem pretty complex. So let me break down the formula.
Step 1: Iterate through each Gallery Item
Step 2: I used the With construct to extract the content of each uploaded file as base64 content using JSON function.
Step 3: As a final step, I will go ahead and use the Patch function to create a Note record in DataVerse. Observe the usage of the Mid function. I have used this function to remove any unwanted characters to upload just the base64 content.
The JSON function return content in the following format.
We need to take the content after base64,. The Mid function accomplish the same.
All set and done. When I click on the button, the Notes are successfully created in DataVerse. And don’t forget to hide the Gallery control.
I hope this helped. You will also like the below posts.
Debajit Dutta
Discover more from Debajit's Power Apps & Dynamics 365 Blog
Subscribe to get the latest posts sent to your email.