function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
FengFeng 

Any one know ways to diplay Attachment in Visualforce page

Hi there,
 
I Save my email attachment(a smal pdf file) into the attachment object, now i want to display the attachment body in my VisualForce Page,
 
<apex: outputfield value="{!Attachment.Body}" /> Not works coz it is base64 field...
 
 
 
any way to display the pdf file in my page? any code example of that, like 's-control' or something
 
 
Thanks in advance
 


Message Edited by Feng on 05-25-2008 05:09 PM
Best Answer chosen by Admin (Salesforce Developers) 
dchasmandchasman
Code:
<apex:repeat var="attachment" value="{!contact.attachments}">
  <apex:iframe src="/servlet/servlet.FileDownload—file={!attachment.id}"/>
</apex:repeat>

You could also leverage <object> and <embed> like this (each approach has pros/cons)

Code:
<object data="/servlet/servlet.FileDownload—file={!attachment.id}" type="application/pdf" width="100%" height="300px">
  <embed src="/servlet/servlet.FileDownload–file={!attachment.id}" width="100%" height="300px"/>
</object>

or even better - create your own custom component to encapsulate the ugly bits (NOTE: you'll need to be using Summer '08 for this specific component to work (there is a bug in Spring '08 that will block you)):

Code:
<apex:component >
    <apex:attribute name="height" type="String" description="TODO: Describe me"/>
    <apex:attribute name="width" type="String" description="TODO: Describe me"/>
    <apex:attribute name="value" type="String" description="TODO: Describe me"/>

    <object data="/servlet/servlet.FileDownload—file={!value}" type="application/pdf" width="{!width}" height="{!height}">
        <embed src="/servlet/servlet.FileDownload–file={!value}" width="{!width}" height="{!height}"/>
    </object>
</apex:component>

 then your page just contains

Code:
<apex:repeat var="attachment" value="{!contact.attachments}">
   <c:pdf value="{!attachment.id}" width="100%" height="200px"/>
</apex:repeat>



Message Edited by dchasman on 05-26-2008 09:23 AM

All Answers

dchasmandchasman
Code:
<apex:repeat var="attachment" value="{!contact.attachments}">
  <apex:iframe src="/servlet/servlet.FileDownload—file={!attachment.id}"/>
</apex:repeat>

You could also leverage <object> and <embed> like this (each approach has pros/cons)

Code:
<object data="/servlet/servlet.FileDownload—file={!attachment.id}" type="application/pdf" width="100%" height="300px">
  <embed src="/servlet/servlet.FileDownload–file={!attachment.id}" width="100%" height="300px"/>
</object>

or even better - create your own custom component to encapsulate the ugly bits (NOTE: you'll need to be using Summer '08 for this specific component to work (there is a bug in Spring '08 that will block you)):

Code:
<apex:component >
    <apex:attribute name="height" type="String" description="TODO: Describe me"/>
    <apex:attribute name="width" type="String" description="TODO: Describe me"/>
    <apex:attribute name="value" type="String" description="TODO: Describe me"/>

    <object data="/servlet/servlet.FileDownload—file={!value}" type="application/pdf" width="{!width}" height="{!height}">
        <embed src="/servlet/servlet.FileDownload–file={!value}" width="{!width}" height="{!height}"/>
    </object>
</apex:component>

 then your page just contains

Code:
<apex:repeat var="attachment" value="{!contact.attachments}">
   <c:pdf value="{!attachment.id}" width="100%" height="200px"/>
</apex:repeat>



Message Edited by dchasman on 05-26-2008 09:23 AM
This was selected as the best answer
FengFeng
thanks, it works perfectly
Cool_DevloperCool_Devloper

Hi DChasman,

 

I tried another method to display the attachment related to my custom object on a VF screen.

<apex:outputLink onclick="window.open('https://cs2.salesforce.com/servlet/servlet.FileDownload?file={!act.CostAttachmentId}','Attachments');" rendered="{!IF(act.CostAttachmentId <> null,true,false)}"> View</apex:outputLink>

 

Its running fine in Mozilla(FireFox) but somehow, in IE the link of the main VF page gets broken when this new window is opened.

 

I have no clue as to why is this happening :(

 

Can you please help me out??

 

Thanks,

Cool_D

DrMKiDrMKi

Great, this is exactly what I was looking for.

 

Unfortunately while trying out, it gives me just white space in Safari and Chrome. In Firefox, it says "plugin not supported". Any idea? 

SMasterSMaster

Sir,

 

Please help me to display an attachment on Visualforce page..

goabhigogoabhigo

Wow. I was about to post about this. Thanks.

nickwick76nickwick76

This was a while ago, but I had the same problem and solved it by doing like this:

 

<apex:outputLink value="https://cs2.salesforce.com/servlet/servlet.FileDownload?file={!act.CostAttachmentId}" rendered="{!IF(act.CostAttachmentId <> null,true,false)}" target="_blank">
View</apex:outputLink>

 This way it will work both in Chrome, FF and IE.

 

// Niklas

rajjjjrajjjj

 

Have anyone tried of displaying a pdf file in vf page without saving as an attachment in salesforce. I need to display a response which comes in the base64 format.

 

EmirEmir

Hello,

 

I have the same problem and need help.

I got file in xml respose from web service and have base64 type:

           

           Folder fldr = newFolder();

            fldr = [select id fromFolderwhereName = 'Temporary'limit 1][0];

           

           Document currDoc = newDocument();

            currDoc.AuthorId = UserInfo.getUserId();

            currDoc.Body = Blob.valueOf(doc.Document.File);

            currDoc.Name = DocumentID;

            currDoc.FolderId = fldr.Id;

            currDoc.ContentType ='application/pdf';

            insert currDoc;

            PageReference page = new PageReference('/servlet/servlet.FileDownload?file=' + currDoc.Id);

 

I see new file in GUI but when I try to open it I got error "System can't read pdf file' or so.

Thanks for any advice.

EmirEmir

And finally I found what I did wrong (should be):

 

            currDoc.Body = EncodingUtil.base64Decode(doc.Document.File);

 

yogesh goadyogesh goad
hi everbody,

the link mentioned in above answers doesnt work now.
it downloads the attached file in a unknown format without extension.
i am talking aboutthe below link
/servlet/servlet.FileDownload–file={!attachment.id}