Created
October 3, 2017 02:55
-
-
Save brianmfear/e6d7f828ea72c667dc5f3b0fe044da4f to your computer and use it in GitHub Desktop.
Example Image Uploader in Visualforce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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