I have a simple inputfile tag. Is it possible to to show the filesize after selection


 <apex:inputFile id="inputfile" value="{!newAtt.body}" filename="{!newAtt.name}"/><br/>

Something like

<script type="text/javascript">
  • 1
    You'd want do to this in JavaScript, since the value would no longer be valid after a file upload. – sfdcfox Dec 11 '18 at 21:58
  • @sfdcfox How would I achieve this, I updated my question with what I tried. – Thomas Dec 11 '18 at 22:13

You can do this by handling the onchange event for the apex:inputFile element.

Update your apex:inputFile to add the handler and add a container to hold the size:

<apex:inputFile id="inputfile" value="{!newAtt.body}" filename="{!newAtt.name}" onchange="handleFileChange(this)/>
Size: <span id="size"></span>

And then add a script to handle the event:

<script type="text/javascript">
    var handleFileChange = function (input) {
        if (input.files && input.files.length > 0) {
            var size = input.files[0].size;
            document.getElementById("size").innerText = size;

When a file is selected, the handler will retrieve the size and set the text of the placeholder span to whatever the file size is.

|improve this answer|||||

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.