Files Upload using apex inputFile in Salesforce Visualforce Page

Files Upload using apex inputFile in Salesforce Visualforce Page

Sample code:
Visualforce Page:
<apex:page standardController=”Account” extensions=”FileUploadController”>
    <apex:form enctype=”multipart/form-data”>
        <apex:variable var=”rowNum” value=”{!0}”  />
        <apex:outputPanel id=”docTable”>
        <apex:outputPanel rendered=”{!refreshPage}”>
           <script>
              window.top.location=’/{!accountId}’;
           </script>
        </apex:outputPanel>
            <table cellspacing=”5″ cellpadding=”5″>   
                <tr style=”{!IF( testDocBool = false, ‘display:block’, ‘display:none’ )}”>
                    <td>Upload Test File</td>
                    <td><apex:inputFile value=”{!testDoc}”/></td>
                </tr>
                <apex:variable var=”rowNum” value=”{!0}”  />
                <apex:repeat value=”{!listAttachments}” var=”att”>
                    <tr style=”{!IF( att.Name =’Test.txt’, ‘display:block’, ‘display:none’ )}”>
                        <td>{!att.Name}</td>
                        <td>
                            <apex:actionRegion >
                                <apex:commandLink value=”Remove” reRender=”docTable” action=”{!delRow}”>
                                    <apex:param value=”{!rowNum}” name=”index” />
                                </apex:commandLink>
                            </apex:actionRegion>
                        </td>
                    </tr>
                    <tr style=”{!IF( att.Name =’Test1.txt’, ‘display:block’, ‘display:none’ )}”>
                        <td>{!att.Name}</td>
                        <td>
                            <apex:actionRegion >
                                <apex:commandLink value=”Remove” reRender=”docTable” action=”{!delRow}”>
                                    <apex:param value=”{!rowNum}” name=”index” />
                                </apex:commandLink>
                            </apex:actionRegion>
                        </td>
                    </tr>
                    <apex:variable var=”rowNum” value=”{!rowNum+1}”/>
                </apex:repeat>
                <tr style=”{!IF( test1DocBool = false, ‘display:block’, ‘display:none’ )}”>
                    <td>Upload Test1 File</td>
                    <td><apex:inputFile value=”{!test1Doc}”/></td>
                </tr>
                <tr>
                    <apex:actionRegion >
                        <td><apex:commandButton value=”Save” action=”{!saveFiles}”/></td>
                        <td><apex:commandButton value=”Cancel” action=”{!cancelChanges}”/></td>
                    </apex:actionRegion>
                </tr>
            </table>
        </apex:outputPanel>
    </apex:form>
</apex:page>                                       


Apex Class:
public class FileUploadController {

    public List < Attachment > listAttachments { get; set; }
    public List < Attachment > listAttachmentsForDel { get; set; }
    public Boolean testDocBool { get; set; }
    public Boolean test1DocBool { get; set; }
    public Blob testDoc { get; set; }
    public Blob test1Doc { get; set; }
    public Id accountId { get; set; }
    private ApexPages.StandardController stdController;
    public Boolean refreshPage { get; set; }

    public FileUploadController(ApexPages.StandardController controller) {

        stdController = controller;    
        refreshPage = false;        
        testDocBool = false;
        test1DocBool = false;
        listAttachments = new List < Attachment >();
        listAttachmentsForDel = new List < Attachment >();
        accountId = controller.getId();
        listAttachments = [ SELECT Id, Name FROM Attachment WHERE ParentId =: accountId ORDER BY Name ];
        
        if ( listAttachments.size() > 0 ) {
        
            for ( Attachment objAttachment : listAttachments ) {
            
                if ( objAttachment.Name == ‘Test.txt’ ) {
                
                    testDocBool = true;
                    
                } else if ( objAttachment.Name == ‘Test1.txt’ ) {
                
                    test1DocBool = true;
                    
                }
            
            }
        }

    }
    
    public void delRow() {
    
        Integer rowNum = Integer.valueOf( ApexPages.currentPage().getParameters().get( ‘index’ ) );
        system.debug( ‘Del Row Num is ‘ + rowNum );
        Attachment currAttachment = listAttachments.get( rowNum );
        listAttachmentsForDel.add( currAttachment );
        
        if ( currAttachment.Name == ‘Test.txt’ ) {
                
            testDocBool = false;
            
        } else if ( currAttachment.Name == ‘Test1.txt’ ) {
        
            test1DocBool = false;
            
        }
        
        listAttachments.remove( rowNum );  
        
    }  
    
    public void saveFiles() {
           
        List < Attachment > listAttachmentsForInsert = new List < Attachment >();
        System.debug( ‘testDoc is ‘ + testDoc );
        System.debug( ‘test1Doc is ‘ + test1Doc );
        
        if ( testDoc != null ) {
                
            Attachment a = new Attachment();
            a.ParentId = accountId;
            a.Name = ‘Test.txt’;
            a.ContentType = ‘text/plain’;
            a.Body = testDoc;
            listAttachmentsForInsert.add( a );
            
        }
        
        if ( test1Doc != null ) {
                
            Attachment a = new Attachment();
            a.ParentId = accountId;
            a.Name = ‘Test1.txt’;
            a.ContentType = ‘text/plain’;
            a.Body = test1Doc;
            listAttachmentsForInsert.add( a );
            
        }
    
        if ( listAttachmentsForInsert.size() > 0 ) {
            
            insert listAttachmentsForInsert;
            
        }
    
        if ( listAttachmentsForDel.size() > 0 ) {
            
            delete listAttachmentsForDel;
            
        }
        
        cancelChanges();
    
    }
    
    public void cancelChanges() {
    
        refreshPage = true;
        
    }

}


Output:

Leave a Reply