Skip to content

Instantly share code, notes, and snippets.

@brianmfear
Created October 3, 2017 02:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brianmfear/e6d7f828ea72c667dc5f3b0fe044da4f to your computer and use it in GitHub Desktop.
Save brianmfear/e6d7f828ea72c667dc5f3b0fe044da4f to your computer and use it in GitHub Desktop.
Example Image Uploader in Visualforce
public class PageController {
// Transient so we don't exceed view state limits
public transient String fileType { get; set; }
public transient Blob fileBody { get; set; }
ApexPages.StandardController controller;
public PageController(ApexPages.StandardController controller) {
this.controller = controller;
}
public void saveProductImage() {
Product2 p = [select id from product2 where id = :controller.getId() for update];
p.RTA__c = '<img src="data:'+fileType+';base64,'+EncodingUtil.base64Encode(fileBody);
update p;
// Get the updated URL so it appears on the page
p = [select rta__c from product2 where id = :p.id];
controller.getRecord().put('RTA__c', p.RTA__c);
}
}
<apex:page standardController="Product2" extensions="PageController">
<apex:form>
<apex:pageBlock>
<apex:pageBlockButtons>
<apex:commandButton value="Update" action="{!saveProductImage}" />
</apex:pageBlockButtons>
<apex:pageBlockSection>
<apex:inputFile contentType="{!fileType}" value="{!fileBody}" />
</apex:pageBlockSection>
<apex:pageBlockSection title="Existing Image" columns="1" collapsible="false">
<apex:outputField value="{!Product2.RTA__c}" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment