3

Situation

I've implemented a simple VF form:

<apex:form>
    <apex:inputFile value="{!aFile}" />
    <apex:commandButton value="send" action="{!upload}" />
</apex:form>

Question

Is it possible to limit size (without hitting governor limits)?

  • Are you looking for a javascript solution? – Phil B Sep 9 '14 at 18:23
  • Looks like Javascript is the only solution. I'm looking for a convenient way to disallow files bigger than 10 mb. Maybe you have an idea. – Christian Deckert Sep 10 '14 at 7:20
  • I posted a javascript solution, hopefully its helpful – Phil B Sep 10 '14 at 17:17
3

Here's a solution using javascript. I'm also using jquery but you could modify it to select the apex:inputfile by its full ID and use vanilla javascript. It's using 500k as an arbitrary limit, but you can set it to whatever you want. The 'attachmentID' is the ID set on the apex:inputfile :

if ($("[id$='attachmentID']")[0].files.length > 0) {
    //validate file attachment is not > 500k
    var uploadFileSize = $("[id$='attachmentID']")[0].files[0].size;
    if (uploadFileSize > 1024 * 1024 * .5) {
        alert('File uploads must be less than 500k in size. Your file is: '+(Math.round(uploadFileSize/1024)) + 'k');
    }
    else {
        // call save method, passed validation
    }
}
else {
    // no file attached, if not required, call save method
}
0

The <apex:inputFile> tag has a fileSize attribute. You could do some logic in your upload() method and throw an exception if the file size is greater than the whatever limit you want to impose.

Here are the docs on <apex:inputFile>

https://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_inputFile.htm

  • 1
    Thanks for your answer. But in case you upload more than 10 mb an internal error will occur. Therefore this isn't a solution. – Christian Deckert Sep 10 '14 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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