我试图单独为每个联系人保存和显示图像。我能够在各自的联系人处成功保存图像。但是,当我刷新页面时,我附上的照片没有显示。 这是代码:
<apex:page standardController="Contact" extensions="photo_attachmentcls">
<apex:pageBlock >
<apex:form >
<apex:inputFile value="{!attach}" fileName="{!fileName}"></apex:inputFile>
<apex:commandButton value="Load" action="{!loader}" />
<apex:actionSupport event="onchange" />
</apex:form>
<apex:outputPanel id="iidd1">
<img id="theImage" src="/servlet/servlet.FileDownload?file={!dsa}" width="100" height="100" />
</apex:outputPanel>
</apex:pageBlock>
</apex:page>
public class photo_attachmentcls {
public Attachment asd{get;set;}
public string purl{get;set;}
public blob attach{get;set;}
public String fileName{get;set;}
public Id recId{get;set;}
public String dsa {get;set;}
public photo_attachmentcls(ApexPages.StandardController ctrl) {
recId = ApexPages.currentPage().getParameters().get('id');
asd = new Attachment();
}
public void loader()
{
asd.body = attach;
asd.Name = fileName;
asd.ParentId = recId;
insert asd;
system.debug('nnnn'+asd);
dsa = asd.id;
system.debug('ddddd'+dsa);
}
}
先谢谢。
答案 0 :(得分:1)
您需要在页面加载时检查该记录是否存在附件:
public class photo_attachmentcls {
public Attachment asd{get;set;}
public string purl{get;set;}
public blob attach{get;set;}
public String fileName{get;set;}
public Id recId{get;set;}
public String dsa {get;set;}
public photo_attachmentcls(ApexPages.StandardController ctrl) {
recId = ApexPages.currentPage().getParameters().get('id');
// check if the contact already has an attachment:
Contact thisRecord = [select id, (Select Id From NotesAndAttachments) from Contact where Id =: recId];
if(!thisRecord.NotesAndAttachments.isEmpty()){
dsa = thisRecord.NotesAndAttachments[0].Id;
} else{
asd = new Attachment();
}
}
public void loader()
{
asd.body = attach;
asd.Name = fileName;
asd.ParentId = recId;
insert asd;
system.debug('nnnn'+asd);
dsa = asd.id;
system.debug('ddddd'+dsa);
}
}
我建议你也选择更好的变量名,但我知道它是某种原型。