Downloadable Attachment Link in Salesforce using Visualforce

Downloadable Attachment Link in Salesforce using Visualforce

Sample code:
Visualforce Page:
<apex:page standardController=”Account” extensions=”FileUploadController”>
    <apex:form enctype=”multipart/form-data”>
        <table>
            <apex:repeat value=”{!listAttachments}” var=”att”>
                <apex:outputLink target=”_self” value=”/servlet/servlet.FileDownload?file={!att.Id}”>{!att.Name}</apex:outputLink>
                <br/>
            </apex:repeat>
        </table>
    </apex:form>
</apex:page>

Apex Class:
public class FileUploadController {
    
    public List < Attachment > listAttachments { get; set; }
    
    public FileUploadController(ApexPages.StandardController controller) {
        
        listAttachments = new List < Attachment >();
        listAttachments = [ SELECT Id, Name FROM Attachment Where ParentId =: controller.getId() ];

    }

}

Output:


Leave a Reply