+ Start a Discussion

Very urgent: Display Attachments in Visualforce Page

Hi All,


Please help to display an attachment in visualforce page...


i have the following Apex Class:


public class PropUpload  
    public Id recId  
    {    get;set;    }  
    public PropUpload(ApexPages.StandardController ctlr)  
       recId = ctlr.getRecord().Id;       
    public string fileName   
    {    get;set;    }  
    public Blob fileBody   
    {    get;set;    }  
    public PageReference UploadFile()  
        PageReference pr;  
        if(fileBody != null && fileName != null)  
          Attachment myAttachment  = new Attachment();  
          myAttachment.Body = fileBody;  
          myAttachment.Name = fileName;  
          myAttachment.ParentId = recId;  
          insert myAttachment;  
             PageReference page = ApexPages.currentPage();
             return page;              
        return null; 



and the following visualforce page:


<apex:page standardController="opportunity_proposals__c" extensions="PropUpload">

 <apex:form >

 <apex:pageblock >

    <div class="upload">


      <td><apex:inputFile value="{!fileBody}" filename="{!fileName}" styleClass="input-file"/></td> 

      <td><apex:commandButton value="Upload Attachment" action="{!uploadFile}"  styleClass="upload-button"/></td>






<apex:pageBlock >

   <apex:repeat value="{!opportunity_proposals__c.attachments}" var="​attachment">

      <embed src="/servlet/servlet.FileDownload–file={!attachment.id}" width="100%" height="20px"/>








Wait what?  You want to show the actual attachment in the Visualforce page inline?  That would only really work if the attachment were HTML -- you can't just show a .doc or an .xls without specialized code to embed the object (and then it would only really work in IE).  Basically you can't just show an arbitrary attachment inline, you have to embed it properly.


Please provide me the code for showing an attachment in the visualforce page.. even as an image...


If you know in advance what type your attachments will be (images, PDFs, HTML pages) then you may be able to show them inline on the page.  If you don't know what they will be then it's very difficult indeed (and for some files and browsers, impossible).


Okay.... Can i just display an attachment name inside visualforce page......


Sure, just query on the attachments in your controller, make a list of their Name fields, and do an apex:repeat on that list and display the names.


Or since you're using a standardController you might just be able to use the related list in your Visualforce, like this (although I'm not certain this tag works with Attachments but it's worth a try


<apex:relatedList list="Attachments" />

I outlined an approach to injecting <img> tags into your VF page with a /servlet... url to display the attachments.   It was in a question about checking the dimensions of uploaded files.  




Best, Steve.

Mike DayMike Day
Hi, sorry it is late but as I came across this I thought I may add a quick note to show how I manage to display attachments within a visualforce page...

<apex:repeat var="att" value="{!Account.Attachments}">
              <apex:image value="/servlet/servlet.FileDownload?file={!att.id}" height="100" width="200"/>

Not sure if this helps 4 years on :)