两种salesforce中展示图片的解决方案 - Code & Without Code

xiaoxiao2021-02-28  99

使用场景:在做练习时候需要使用到图片上传,用来展示会议发言者的个人图片。

准备:

1、创建两个字段,一个Url类型用来接受url,一个返回类型为Text的公式字段用来展示图片;

2、自定义上传图片组件,这里将自定义一个visualforce page;

3、自定义上传图片控制器,这里将自定义一个控制器用来将图片上传到salesforce服务器;

代码片段:

<apex:page standardController="Speaker__c" extensions="SpeakerControllerExtension"> <apex:form > <apex:pageBlock title="Speaker"> <apex:pageBlockSection columns="1"> <apex:inputField value="{!Speaker__c.First_Name__c}"/> <apex:inputField value="{!Speaker__c.Last_Name__c}"/> <apex:inputField value="{!Speaker__c.Email__c}"/> <apex:inputFile value="{!picture}" accept="image/*" /> </apex:pageBlockSection> <apex:pageBlockButtons > <apex:commandButton value="Save" action="{!save}"/> </apex:pageBlockButtons> </apex:pageBlock> {!errorMessage} </apex:form> </apex:page> public class SpeakerControllerExtension { public blob picture { get; set; } public String errorMessage { get; set; } private final Speaker__c speaker; private ApexPages.StandardController stdController; public SpeakerControllerExtension(ApexPages.StandardController stdController) { this.speaker = (Speaker__c)stdController.getRecord();//获得当前页面的记录 this.stdController = stdController;//该步骤作用在于延伸Save按钮方法 } public PageReference save() { errorMessage = ''; try { upsert speaker; if (picture != null) { Attachment attachment = new Attachment(); attachment.body = picture; attachment.name = 'speaker_' + speaker.id + '.jpg'; attachment.parentid = speaker.id; attachment.ContentType = 'application/jpg'; insert attachment; speaker.Picture_Path__c = '/servlet/servlet.FileDownload?file=' + attachment.id; update speaker; } return new ApexPages.StandardController(speaker).view(); } catch(System.Exception ex) { errorMessage = ex.getMessage(); return null; } } } 效果展示:

补充:以上是通过Code的方式实现图片展示,下面将以without Code的方式实现同样的效果:

1、新增一个富文本字段; 2、编辑时选择图片上传即可;

效果预览:

转载请注明原文地址: https://www.6miu.com/read-36611.html

最新回复(0)