When uploading a file via the Visualforce apex:inputFile standard component, if the file size exceeds the 3Mb limit I would like to display a message to the user that the file is greater than 3Mb and cannot be uploaded into the system.
Please advise how this can be achieved on VisualForce level?


Just bind fileSize to an attribute:

public Integer fileSize { get; set; }
// ...
if(fileSize > 3000000) {
  // Show error

<apex:inputFile filesize="{!fileSize}" ... />
| improve this answer | |
  • Shouldn't it be 3 * 1024 * 1024? so if(fileSize > 3145728) – caleb.breckon Mar 13 '18 at 17:06
  • @caleb.breckon I personally prefer the classic definition of kilo/mega/giga (base-2 notation), but Salesforce tends to use the IEC standard definitions (notice we have a 6000000 byte heap limit, not 6291456 bytes). It's really a matter of who's asking. – sfdcfox Mar 13 '18 at 18:18
  • @sfdcfox do we have any limitation for this. I have exactly used this but I am gettng filesize null in submit function called through action function on apex side. The function has return type of pageReference. I am really stuck there ! – Pankaj Andhale Aug 25 at 14:11
  • 1
    @PankajAndhale It depends on the situation. apex:inputFile does not support rerendering, so you need to be aware of the limitations. You may want to ask a specific question with your code so we can take a look. – sfdcfox Aug 25 at 14:35
  • 1
    @PankajAndhale Maybe a bug? I know there is a maximum payload size, it may be that Visualforce is not even attempting the file since it exceeds some limit. – sfdcfox Aug 25 at 16:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.