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
Revathy2690Revathy2690 

Is there a way to View attachment body inside a visualforce page

Hi All,

I am uploading documents into Attachment object and keeping a custom object as parent. I want to show the attachment content inside the visualforce page .
Below is part of my code. But in the page content is not getting displayed, it's blank
Page:
        
        <apex:pageBlockSection title="Resume" id="Resume" >
           <apex:iframe src="/servlet/servlet.FileDownload—file={!resume.id}"/>
        </apex:pageBlockSection>
        
Controller:
public Attachment resume{ get; set;}
resume=[select Id,Body,ContentType,Name from Attachment where ParentId =: currentRecordId];

Can anyone please advice on this ?
Thanks in advance
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi ,

Please use below code in your visualforce.
<apex:image url="/servlet/servlet.FileDownload?file=YOUR_ATTACHMENT_ID"/>
Let us know if it helps you.
Revathy2690Revathy2690
Hi Ashish,

Thanks for helping. I tried that already attachment is not loading. And the attachment I am trying to show is word doc.
Vla ChaVla Cha
Hi Revathy2690,

maybe you have problems with the property definitions in your controller. I've used following code in order to verify the solution:

Property definition in controller (assuming that account has one attachment):
public Attachment resume{ get{ return resume=[select Id,Body,ContentType,Name from Attachment where ParentId ='001xxxxxxxxxxxx']; } set;}
Display of pdf doc in iframe as per your requirement:
<apex:pageBlockSection title="Resume" id="Resume" >
           <apex:iframe src="/servlet/servlet.FileDownload?file={!resume.id}"/>
        </apex:pageBlockSection>

I hope this helps :)


 
Revathy2690Revathy2690
Hi,
I tried this, on loading the page it is prompting to download the document. Except for word document code is working as expected.
but what I want is to show the content of word document like a preview :(
Vla ChaVla Cha
Hi,

it's not possible to display word doc in iframe in a way you have this for pdf docs. You will be always prompted to do download the file. You should consider usage of some 3rd party javascript code in order to display the file.

Cheers!
Revathy2690Revathy2690
Can you please suggest any 3'rd party then ? Thank u.