+ Start a Discussion
shabuthomasshabuthomas 

Displaying Notes & Attachments on Visual Force Pages

Would you please help me to display Notes & Attachments on a Visual Force page having CUSTOM controller ?
Is there any tag for it ?

Best Answer chosen by Admin (Salesforce Developers) 
JimPDXJimPDX

Thanks Ron. It ended up being much simpler than I thought:

 

 

<img src="/servlet/servlet.FileDownload?file=00P80000003PXMyEAO

 

 

All Answers

JimRaeJimRae
Use the related List tag.  With a custom controller, you will usually need to set the list name and the subject (id).

Code:
<apex:relatedList> id="noteslist" list="NotesAndAttachments" subject="{!obj.id}" />

 obj.id is the object you want to see the related list for.

Richie DRichie D
You'll need to add the related list to the page layout to see it otherwise you'll get
 
'NotesAndAttachments' is not a valid child relationship name for entity Xxxxx
Had me stuck so thought i'd pass it on ;)
 
JimPDXJimPDX
But what if you want to show the file itself (like a JPG photo) which is an attachment? How do you get the path to the image? Is there a way to feed a parameter into <apex:image> ?
Ron WildRon Wild

You might be able to do it with a data URL:

 

<img src="data:image/gif;base64,R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/f3//Ub/
/ge8WSLf/rhf/3kdbW1mxsbP//mf///yH5BAAAAAAALAAAAAAQAA4AAARe8L1Ekyky67QZ1hLnjM5UUde0ECwLJoExKcpp
V0aCcGCmTIHEIUEqjgaORCMxIC6e0CcguWw6aFjsVMkkIr7g77ZKPJjPZqIyd7sJAgVGoEGv2xsBxqNgYPj/gAwXEQA7"
width="16" height="14" alt="embedded folder icon">

You would just embed the mime type and base64 encoded content of the attachment.

 

The only problem is that while Firefox and others support data URLs, IE does not (at least right now).

 

 

JimPDXJimPDX

Thanks Ron. It ended up being much simpler than I thought:

 

 

<img src="/servlet/servlet.FileDownload?file=00P80000003PXMyEAO

 

 

This was selected as the best answer
Ron WildRon Wild

That works inside Salesforce, but apparently not on a public Sites VF page.  The data url has worked for me, but again IE doesn't support data urls and the next version IE 8.0 will only support data urls up to 32KB in length.

 

I'm curious, has anyone been able to push a word/excel/pdf (non-public images) to a public Sites page?

 

I've tried setting the <apex:page ...> attribute to a specific mime type, but getting the attachment body (base64 content) into the page hasn't been as easy as I thought it would be.

 

 

richardvrichardv

I'm having a similar issue.  I have a "ViewImage" VF page and I obviously want it to render as an image.  While I'm close, it looks like Apex/VF isn't handling upper ASCII strings correctly.  Here's my code:

 

VF Page

====================== 

<apex:page showHeader="false" sidebar="false" controller="ViewImageCtrl" contentType="image/gif">{!data}</apex:page> 

 

Controller

====================== 

public class ViewImageCtrl {

public String data {get;set;}

public ViewImageCtrl(){

//the string below is base 64 encoded version of the image availabe at /s.gif

data = EncodingUtil.base64Decode('R0lGODlhAQABAID/AMDAwAAAACH5' + 'BAEAAAAALAAAAAABAAEAAAICRAEAOw==').toString();

}

}

 

 

The real image has the following byte sequence:

4749 4638 3961 0100 0100 80FF 00C0 C0C0 0000 0021 F904 0100 0000 002C 0000 0000 

0100 0100 0002 0244 0100 3B

 

The image rendered by my VF page has the following incorrect byte sequence (NOTE they are similar but this version is longer): 

4749 4638 3961 0100 0100 EFBF BDEF BFBD 00EF BFBD EFBF BDEF BFBD 0000 0021 EFBF 

BD04 0100 0000 002C 0000 0000 0100 0100 0002 0244 0100 3B 

 

The first diff is the 11th byte sequence - the real version has 80 which is in the upper ASCII range of bytes.  Its my hunch that Apex is escaping this byte because it doesn't recognize it.

 

Thoughts?  Workarounds?

 

Note to SF Prod Mgmt: VF pages need to be able to more easily return binary streams of data.  Ideally you should have some sort of "new" root tag (maybe <apex:binary> or <apex:file>) which makes this sort of functionality explicit with a tag that accepts only Blob fields.

 

-Richard Vanhook 

 

Message Edited by richardv on 04-08-2009 07:23 AM
richardvrichardv

After looking at the byte sequences a little more, its pretty obvious that any byte > 127 gets escaped to three bytes EF BF BD.  For example, the sequences stacked on top of each other starting at 11th byte:

 

11     12      13 14      15     16     17

80     FF      00 C0      C0     C0     0000
EFBFBD EFBFBD  00 EFBFBD  EFBFBD EFBFBD 0000 

Message Edited by richardv on 04-08-2009 07:34 AM
Message Edited by richardv on 04-08-2009 07:34 AM
Ron WildRon Wild

This is just a stab in the dark, but would <apex:outputText escape="false" ....   work?

 

 

Ron

richardvrichardv

Cliff McQuirter gave me the following solution to dynamically rendering attachments in Sites (thanks Cliff!):
 

============================
Page:
============================
<apex:page standardController="Account" extensions="InputFileControllerExtension">
    <apex:messages />
    <apex:form id="theForm">
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}"/>
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
    <apex:pageBlock >
        <apex:repeat value="{!account.attachments}" var="attachment">
            <apex:image height="200px" value="{!URLFOR($Action.Attachment.Download, attachment.Id)}"/><p/>
        </apex:repeat>
    </apex:pageBlock>
</apex:page>

 
 
============================
Controller
============================
public class InputFileControllerExtension {
 
    ApexPages.StandardController stdController;
    private final Account acct;
 
    public Attachment attachment {
        get {
            if (attachment == null) attachment = new Attachment();
            return attachment;
        }
 
        set;
    }
 
    public InputFileControllerExtension(ApexPages.StandardController stdController)
    {
        this.acct = (Account)stdController.getRecord();
        this.stdController = stdController;
    }
 
    public PageReference save() {
        attachment.parentid = acct.id;
        insert attachment;
       
        PageReference page = ApexPages.currentPage();
        page.setRedirect(true);
        return page;
     }
}

 
============================
Config Steps:
============================
a. Go to App Setup > Develop > Sites > [your site] > Public Access Settings
b. Scroll down to Standard Object Permissions. Check to see whether your object has read permission (by default, only Ideas has Read checked).
c. Edit - add Read permission to your object 
 
 

melchisholm22melchisholm22

 Here's another example of dynamically displaying image attachments. I hope this helps.

 

 

//Controller:

public List<Attachment> postImgId;

String qp;

 

public SampleController()

{

qp = ApexPages.currentPage().getParameters().get('id');

postImgId = [Select Id From Attachment where ContentId = :qp limit 1];

}

 

public string getPostImgId()

{

String strImgLink = '/servlet/servlet.FileDownload?file=' + postImgId[0].Id;

return strImgLink;

}

 

<!--VF markup: -->

<apex:image value="{!PostImgId}" />

 


 

Message Edited by melchisholm22 on 10-05-2009 03:28 AM
EmsEms
Thanks for posting this code, it was exactly what I needed to do!
SMasterSMaster

please provide the complete code to display the attached files in VF Page

Sumit KumarSumit Kumar

 

Please help me getting this error

 Error: Missing required attribute list in <apex:relatedList> in