2014-04-07 2 views
0

각 연락처의 이미지를 개별적으로 저장하고 표시하려고합니다. 나는 각각의 접촉에서 이미지를 성공적으로 저장할 수 있었다. 하지만, 제가 사진을 첨부했을 때 사진이 표시되지 않습니다. 사전에 salesforce에서 이미지를 저장할 수 없습니다.

<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); 

    }  
} 

감사 : 다음은 코드입니다.

답변

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); 

    }  
} 

나뿐만 아니라 더 나은 변수 이름을 선택하는 것이 좋습니다 것입니다,하지만 난 어떤 종류의 프로토 타입입니다 인식 .

+0

Thnaks @geekymartian. – Rv1

+0

당신을 환영합니다! – geekymartian

관련 문제