function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bblaybblay 

Limiting File Size

I was curious if someone might be able to help me limit the attachment size a user is allowed to upload on my VF page to only 2MB. I found this code online and have no idea where to start. I'm thinking it would likely just be a couple lines of code to check the file size and display an error message if more than 2MB but I'm not sure how to do this. 

 

Any ideas or help would certainly be appreciated! Thanks!

 

 

public class VFFileUpload  
{  
    public Id recId {get;set;}  
      
    public VFFileUpload(ApexPages.StandardController ctlr)  
    {  
       recId = ctlr.getRecord().Id;       
    }  
      
    public string fileName {get;set;}  
      
    public Blob fileBody {get;set;}  
    
    private integer fileSize;
    
    public Blob getFileBody()
    {
        return this.fileBody;
    }
    
    public void setFileBody(Blob fileBody)
    {
        this.fileBody = fileBody;
        setFileSize(this.fileBody.size());
    }

    public Integer getFileSize()
    {
        return this.fileSize;
    }

    public void setFileSize(Integer fileSize)
    {
        this.fileSize = fileSize;
    }
                                                     
    public PageReference UploadFile()  
    {  
        PageReference pr;  
        if(fileBody != null && fileName != null)  
        {  
        
          Attachment myAttachment  = new Attachment();  
          myAttachment.Body = fileBody;  
          myAttachment.Name = fileName;  
          myAttachment.ParentId = recId;  
          insert myAttachment;  
  
          Member__c m = [select id from Member__c WHERE     ID=:System.currentPageReference().getParameters().get('id')];
          List<Attachment> a = [select id from attachment where parentid = :m.id];
 
 
          if(a.size() > 7){
            delete a;         
          }                                   
                   
          PageReference page = ApexPages.currentPage();
          page.setRedirect(true);
          return page;    
        }  
        return null; 
    }    
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You can retrieve the blob size in characters which should give you a reasonable approximation.  E.g.

 

 

public PageReference UploadFile()  
    {  
        PageReference pr;  
        if(fileBody != null && fileName != null)  
        {  
        
          if (fileBody.size()>2097152)
          {
              ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Max attachment size is 2Mb));
          }
          else
          {
            Attachment myAttachment  = new Attachment();  
            myAttachment.Body = fileBody;  
            myAttachment.Name = fileName;  
            myAttachment.ParentId = recId;  
            insert myAttachment;  
  
            Member__c m = [select id from Member__c WHERE     ID=:System.currentPageReference().getParameters().get('id')];
            List<Attachment> a = [select id from attachment where parentid = :m.id];
 
 
            if(a.size() > 7){
              delete a;         
            }                                   
                   
            PageReference page = ApexPages.currentPage();
            page.setRedirect(true);
            return page;    
          }  
        }
        return null; 
    }    

 

 

All Answers

bob_buzzardbob_buzzard

You can retrieve the blob size in characters which should give you a reasonable approximation.  E.g.

 

 

public PageReference UploadFile()  
    {  
        PageReference pr;  
        if(fileBody != null && fileName != null)  
        {  
        
          if (fileBody.size()>2097152)
          {
              ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Max attachment size is 2Mb));
          }
          else
          {
            Attachment myAttachment  = new Attachment();  
            myAttachment.Body = fileBody;  
            myAttachment.Name = fileName;  
            myAttachment.ParentId = recId;  
            insert myAttachment;  
  
            Member__c m = [select id from Member__c WHERE     ID=:System.currentPageReference().getParameters().get('id')];
            List<Attachment> a = [select id from attachment where parentid = :m.id];
 
 
            if(a.size() > 7){
              delete a;         
            }                                   
                   
            PageReference page = ApexPages.currentPage();
            page.setRedirect(true);
            return page;    
          }  
        }
        return null; 
    }    

 

 

This was selected as the best answer
bblaybblay

This did exactly what I needed it to do! Thank you so much for your quick help! That's what I love about the Force.com boards, everyone is so friendly and willing to help out others :)