limit size of attachments uploaded through file component?

  • 1
  • Question
  • Updated 4 years ago
  • Answered
We've got 4 custom lookup fields on a detail object, which each relate to a specific image of the vehicle being stored (interior image, exterior image etc). We want to prevent users from uploading files larger than e.g. 500kb, (for faster page loads when previewing images, and these images will be transferred onto a document)

Would it be possible to limit the size of attachments uploaded through the file component?
Photo of Greg Jarrett

Greg Jarrett

  • 3,496 Points 3k badge 2x thumb

Posted 4 years ago

  • 1
Photo of Irvin Waldman

Irvin Waldman, Champion

  • 9,006 Points 5k badge 2x thumb
You could write a Before Insert on Attachment trigger and restrict the body size.  Don't know if Skuid provides access to the file properties in the JS API.
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
Greg,   here are some answers from our Dev Team: 
We don't provide an out of the box way to limit the file size. Normally, you'd want this sort of validation to be handled server side, because anything you rig up client side in JavaScript could be manipulated before submit to get around your validation. Of course, if this is all for an internal app where you can say, "Come on, folks. Play nice" this may not be an issue. 

A trigger on Attachment like Irvin suggested could work, but you'll need to get pretty selective with how it's enforced, lest you block other Attachments larger than 500 KB that you do want to allow.  

You could also take a look at this conversation.  https://community.skuidify.com/skuid/topics/how_to_upload_images_photos_efficently
It doesn't solve all your questions, but does offer some cues. 
Photo of Irvin Waldman

Irvin Waldman, Champion

  • 9,006 Points 5k badge 2x thumb
trigger AttachmentFilter on Attachment bulk (before insert) {
    // Get max attachment size from our custom settings
    XYZ_Settings__c settings = XYZ_Settings__c.getInstance();
    Integer MaxAttachmentSize = settings.Max_Attachment_Size;
    
    // Disallow any attachment from Rob or exceeding max size limit
    for (Attachment a : Trigger.new) {
        if (UserInfo.getName() == 'Rob Hatch') {
            a.addError('Skipping attachment since Rob is in the doghouse, again.');
        } else if (a.BodyLength > MaxAttachmentSize) {
            a.addError('Skipping attachment too large per MAX_ATTACHMENT_SIZE.');
        } 
    }
}
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
Ummm,  seriously?  Again? 

Photo of Greg Jarrett

Greg Jarrett

  • 3,496 Points 3k badge 2x thumb
Irvin that's brilliant. Definitely the simplest most immediate solution. The only question is do I leave the reference to Rob in there for my future dev team to ponder over - 'who's this Rob guy?'

Long term it would be great to allow users to crop/adjust images themselves during the upload as per your post Rob - the images our users put in will be merged into drawloop documents so making it easy for them to maintain consistent dimensions and image sizes would be a huge bonus.
Photo of Irvin Waldman

Irvin Waldman, Champion

  • 9,006 Points 5k badge 2x thumb
Sorry, could not resist... was in meetings all day and I needed a little levity to break up the monotony. 
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
No worries at all...