Spread the love

This post is a recap of this Salesforce Developer Forum thread.

We want to trigger a download from an attachment but the running user don’t have access to the object (think of Community User for instance).

In the controller read the Attachment’s body in a String getter coded in Base64:

public String base64Value{get;set;}
public String contentType{get;set;}
public void loadAttachment(){
   Attachment att = [Select Id, Body, ContentType From Attachment limit 1];
   base64Value = EncodingUtil.base64Encode(att.Body);
   contentType = att.ContentType;
}

In the page:

<a href="data:{!contentType};content-disposition:attachment;base64,{!base64Value}">Download file</a>