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
SMasterSMaster 

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();
             page.setRedirect(true);
             return page;              
        }  
        return null; 
    }          
}

 

 

and the following visualforce page:

 

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

 <apex:form >

 <apex:pageblock >

    <div class="upload">

    <table><tr>

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

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

    </tr></table>

   </div> 

   </apex:pageblock>

</apex:form>

 

<apex:pageBlock >

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

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

                </apex:repeat>

</apex:pageBlock>

   

  

 

 </apex:page>

*werewolf**werewolf*

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.

SMasterSMaster

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

*werewolf**werewolf*

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

SMasterSMaster

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

*werewolf**werewolf*

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.

*werewolf**werewolf*

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" />
SteveBowerSteveBower

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.  

 

http://boards.developerforce.com/t5/Apex-Code-Development/Validating-Image-Dimensions-width-and-height-of-an-image-saved/m-p/224045#M39566

 

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"/>
            </apex:repeat>

Not sure if this helps 4 years on :)