Data

Show size of attachment or uploaded file

Have you ever wanted to show or use the size of an uploaded file – the process is below

Attachment

Firstly, you need a hidden image control with the Image

Last(AttachmentControlName.Attachments).Value

Then on the OnAddFile (and OnRemoveFile if you want)

UpdateContext(
   {
      varJSON: 
      JSON(
         YourHiddenImageName.Image,
         JSONFormat.IncludeBinaryData
      )
   }
)

Then the Label below has the Text

"Last Attachment: " & (Len(varJSON) - 814) / 1370000 & " Mb"

Uploaded file

A similar, but less complex process – on the OnChange of the AddMediaButton

UpdateContext(
    {
      varJSON: 
      JSON(
         UploadedImageName.Image,
         JSONFormat.IncludeBinaryData
      )
   }
)

and the Label below

"Image Size: " & (Len(varJSON) - 814) / 1370000 & " Mb"

All attachments on a record

An expansion on #1 above – make a Gallery (called galAttach below) with the Items

AttachmentControlName.Attachments

Now put an Image control in the Gallery (called imAttach below) with the Image

ThisItem.Value

You can now hide the gallery

You will need to trigger the next piece, so a button with “Size” or similar below the attachment control will do the job. If you want to make it dynamic instead, put this code at the point you display the Form (maybe Screen OnVisible) and also on the Attachment control OnAddFile, OnRemoveFile and OnUndoRemoveFile.

ForAll(
   galAttach.AllItems,
   Collect(
      colSize,
      {
         ImSize: 
         (Len(
            JSON(
               imAttach.Image,
               JSONFormat.IncludeBinaryData
            )
         ) - 814) / 1370000
      }
   )
);
UpdateContext(
   {
      varSize: 
      Sum(
         colSize,
         ImSize
      )
   }
)

A collection is being made here containing a calculation of the size of the JSON text conversion of each image, then a Variable is being set to the total of all of these values, so you can then have a Label with

"Total Attachment size: " & varSize & " Mb"

7 Comments

  • Sai Teja

    I am getting the file size using the above formula. But when I upload a text which is less than 1 KB, it is giving me a negative value. Ideally it should not right?

  • Bella

    Hello,
    I notice there wa s issue for me and others o how to clear the Colsize collection, just wanted to comment how I fix it:
    1. OnVisible = ClearCollect(colSize,{ImSize: 0});
    2. Add a 10 second timer to autostart= true, OnTimerEnd = put calculation focumla along with ClearCollect(colSize,{ImSize: 0}); at the end.
    2. OnAddFile & OnRemoveFile = 1st Put ClearCollect(colSize,{ImSize: 0}); then the calculation formula.

    timer can be more or less depending the amount of attahcmnets, I notice PowerApps needs time to loop through.

    Thansk again for the great help!

Leave a Reply

Your email address will not be published. Required fields are marked *