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?
-
@ Sebastian, so far I've tried the script in the following article: bobbuzzard.blogspot.bg/2014/09/…– lemdevCommented Mar 14, 2018 at 12:24
Add a comment
|
1 Answer
Just bind fileSize to an attribute:
public Integer fileSize { get; set; }
// ...
if(fileSize > 3000000) {
// Show error
return;
}
<apex:inputFile filesize="{!fileSize}" ... />
-
Shouldn't it be 3 * 1024 * 1024? so
if(fileSize > 3145728)
Commented Mar 13, 2018 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 ♦Commented Mar 13, 2018 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 ! Commented Aug 25, 2020 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 ♦Commented Aug 25, 2020 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 ♦Commented Aug 25, 2020 at 16:13