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
Prafull G.Prafull G. 

Client side file size validation for <apex:inputFile ............



We are having a issue with the File Input (attachment). Issue description is described below.


In the visualforce page, we have made a validation on the attachment for the size of 2 MB. Now conditions are as below:


          (i) If the uploaded attachment has size less than or equal to 2 MB then it works properly and attachment insertion proceed successfully.

          (ii) If the uploaded attachment has size greater than 2 MB and upto 25 MB then it gives proper error message from the controller.

          (iii) Issue arise in this situation. As the uploaded attachment size is greater then 25 MB then salesforce gives it's own error message 'Error : This file exceeds the maximum size limit of 25 MB.' and makes attachment null.


I tried to solve this on the client side with the jQuery. jQuery code worked properly in the html page and I was able to get the full filepath but as I merged that code in visualforce page then I just get the filename, not the exact file path and so unable to get size client side.


I want to make 2 MB size validation in all the scenarios.


Please share your views.





To be honest, I'm surprised you were able to find out anything about the file client side - browsers are supposed to restrict that kind of thing so that malicious code can't take advantage of it.


I know that there are flash based jquery plugins that can restrict the file size, but they won't connect with visualforce, as that uses the vanilla (highly restricted) HTML file input element.




Are you checking the file size after it's uploaded?   If before, can you please share the code?



The problem I am having is that SF has a 5MB file size limit, and if a 5MB+ file is selected in the inputFile component, then VF just ignores that file, and sets all properties of associated attachment variable to null.


I want to show users some type of an error, letting them know that the file is not gonna be uploaded because of its size.  


Thanks for help.


Hi All ,

I checked when inserting attchments it depent on apex heap size limit in a single instance . Apex Heap Size limit is 3000000 . Now lets say if your input file size is exceeding 3000000 byte would raise an error . 

So this size comes approximately 2.83 to 2.85 Mb with the total heap size 3000000 byte insert successfully . If exceeding 2.85 raising an error.






Ven 777Ven 777

Was able to handle this server-side as below. This will display this message back on the page:

Error:This file exceeds the maximum size limit of 5MB.


public PageReference saveForm() {
        if(ApexPages.hasMessages(ApexPages.severity.ERROR)) {
            System.debug('Exceeds File Size limit, abort processing. ApexPages.getMessages() ' + ApexPages.getMessages());            
            return null;


      ..  actual file processing logic here...




OK, I have this working:


VF page


<apex:inputFile size="50" filename="{!att0name}" filesize="{!att0size}" value="{!att0body}" />  



if(att0name != null && att0Body != null){
          if(att0size > 5242880){  
            //maximum attachment size is 5MB.  Show an error
            ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.Error, 'A "' + folderName0 + '" file was NOT uploaded, because it is too big (' + Decimal.valueOf(att0size/1048576).setScale(1) + ' MB): ' + att0name));     



Hi Kirill,

If i try the same, its giving me view state error.

Any other way?