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

How to Upload multiple files in the vf Page

I want to upload the multiple files in the vf page & inserted in the document object. Please help me out buddies.
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan
Hi  Siva Kumar,

Use the following sample code to upload a new attachements to Account object record. You can either keep this vf page as inline VF in Account object (or) directly run the page with Id mentioned like (/). You can choose how many number of attachment you would like to add to particular record usign this code.

VF page
<apex:page standardController="Account" extensions="MultipleUploadController">
    <apex:form >
        <apex:pageBlock title="Upload Multiple Attachment to Object">
            <apex:pageBlockButtons >
                <apex:commandButton value="Upload"  action="{!SaveAttachments}"/>
            <apex:pageMessages id="MSG"/>
            <apex:actionFunction name="ChangeCount" action="{!ChangeCount}"/>
            <apex:pageblocksection >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="How many files you want to upload?"/>
                    <apex:selectList onchange="ChangeCount() ;" multiselect="false" size="1" value="{!FileCount}">
                        <apex:selectOption itemLabel="--None--" itemValue=""/>
                        <apex:selectOptions value="{!filesCountList}"/>
            <apex:pageBlockSection title="Select Files" rendered="{!IF(FileCount != null && FileCount != '', true , false)}">
                <apex:repeat value="{!allFileList}" var="AFL">
                    <apex:inputfile value="{!AFL.body}" filename="{!AFL.Name}" />


public class MultipleUploadController
    //Picklist of tnteger values to hold file count
    public List<SelectOption> filesCountList {get; set;}
    //Selected count
    public String FileCount {get; set;}
    public List<Attachment> allFileList {get; set;}
    public MultipleUploadController(ApexPages.StandardController controller)
        filesCountList = new List<SelectOption>() ;
        FileCount = '' ;
        allFileList = new List<Attachment>() ;
        //Adding values count list - you can change this according to your need
        for(Integer i = 1 ; i < 11 ; i++)
            filesCountList.add(new SelectOption(''+i , ''+i)) ;
    public Pagereference SaveAttachments()
        String accId = System.currentPagereference().getParameters().get('id');
        if(accId == null || accId == '')
            ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.ERROR,'No record is associated. Please pass record Id in parameter.'));
        if(FileCount == null || FileCount == '')
            ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.ERROR,'Please select how many files you want to upload.'));

        List<Attachment> listToInsert = new List<Attachment>() ;
        //Attachment a = new Attachment(parentId = accid,, body = myfile.body);
        for(Attachment a: allFileList)
            if( != '' && != '' && a.body != null)
                listToInsert.add(new Attachment(parentId = accId, name =, body = a.body)) ;
        //Inserting attachments
        if(listToInsert.size() > 0)
            insert listToInsert ;
            ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.INFO, listToInsert.size() + ' file(s) are uploaded successfully'));
            FileCount = '' ;
            ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.ERROR,'Please select at-least one file'));
        return null;
    public PageReference ChangeCount()
        allFileList.clear() ;
        //Adding multiple attachments instance
        for(Integer i = 1 ; i <= Integer.valueOf(FileCount) ; i++)
            allFileList.add(new Attachment()) ;
        return null ;

Hope this helps you. Please select this as a Best answer to give kudos, if it suits your requirment so others can make use of it...!

Kamatchi Devi R
Salesforce Certified Developer | Salesforce Certified Administrator | Service Cloud Consultant
Dan NapolitanoDan Napolitano
Kamatchi Devi R:

Will this solution work in Salesforce communities (partner portal)? I'm looking for the best way to allow partner portal users to upload multiple files at once. The usual App Exchange and other 3rd party solutions do not seem to be supported by portals. 

Thanks very much.
Patricia LimPatricia Lim
Very interested in the above question as I am looking to do the same in Communities - Dan did you find a solution to this?