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) / 137000
      }
   )
);
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"

One Comment

Leave a Reply

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