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
vpmvpm 

Display an Image from attachment on Visualforce Page

Hi,

 I want to display an image from attached file on vf page.

 

//vf

<apex:page standardController="stdCust_obj__c" extensions="displayImageExtension">
<table><tr>
<td><h1>Photo:</h1></td>
<td>
<apex:image url="/servlet/servlet.FileDownload?file=file.Id"/>
</td>
</tr>
</table>
</apex:page>

 

//apex

 

public class displayImageExtension {

private id StCustId;
List<Attachment> file;

 

public displayImageExtension(ApexPages.StandardController stdController) {
this.StCustId = ApexPages.currentPage().getParameters().get('id');
}

public List<Attachment> getfile(){

file=[Select Id,Name,LastModifiedDate from attachment where ParentId=:StCustId];
return file; 
}

 

}

 

Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
Puja_mfsiPuja_mfsi

Hi,

Use the below controller and Visual force page  :

 

Apex Class

Public Class displayImageExtension {

    String recId;
    
    public displayImageExtension(ApexPages.StandardController controller) {
        recId = controller.getId();    
    }
    
    public String getFileId() {
        String fileId = '';
        List<Attachment> attachedFiles = [select Id from Attachment where parentId =:recId order By LastModifiedDate DESC limit 1];
        if( attachedFiles != null && attachedFiles.size() > 0 ) {
            fileId = attachedFiles[0].Id;
        }
        return fileId;    
    }
}

 

 

Visual Force Page

 

<apex:page standardController="Account" extensions="AccountImageController">
<apex:form >
<apex:image url="/servlet/servlet.FileDownload?file={!FileId}"/>
</apex:form>
</apex:page>

 

Please let me know If you need any help regarding the same .

 

 

 

All Answers

Vinita_SFDCVinita_SFDC

Hello,

 

Please refer the following example :

 

Controller :
=================================

public with sharing class ImageController {
 public String imageURL{get;set;}
   
  public ImageController()
  {
    imageURL='/servlet/servlet.FileDownload?file=';
    List< document > documentList=[select name from attachment where 
                                    Name='SamplePic'];
   
    if(documentList.size()>0)
    {
      imageURL=imageURL+documentList[0].id;
    }
  }
}
 
Visual Force Page :
=====================

<apex:page controller="ImageController" showheader="false" sidebar="false">
 
    <apex:form>
      <apex:image url="{!imageURL}">
    </apex:image></apex:form>
 
</apex:page>
 
Hope this helps!
vpmvpm

Hi 

Puja_mfsiPuja_mfsi

Hi,

Use the below controller and Visual force page  :

 

Apex Class

Public Class displayImageExtension {

    String recId;
    
    public displayImageExtension(ApexPages.StandardController controller) {
        recId = controller.getId();    
    }
    
    public String getFileId() {
        String fileId = '';
        List<Attachment> attachedFiles = [select Id from Attachment where parentId =:recId order By LastModifiedDate DESC limit 1];
        if( attachedFiles != null && attachedFiles.size() > 0 ) {
            fileId = attachedFiles[0].Id;
        }
        return fileId;    
    }
}

 

 

Visual Force Page

 

<apex:page standardController="Account" extensions="AccountImageController">
<apex:form >
<apex:image url="/servlet/servlet.FileDownload?file={!FileId}"/>
</apex:form>
</apex:page>

 

Please let me know If you need any help regarding the same .

 

 

 

This was selected as the best answer
vpmvpm
Hi Puja_mfsi,

Really thanks... It is working fine. I can see the image in detail page of my object. Once again thanks.
jass aulakhjass aulakh

How to display list of images on visualforce page from attachments/Documents?

 

HobieHobie
Hi Puja_mfsi,

I borrowed your code and it works well in my Sandbox.  Thank you.

Just went to move the change set to Production and got this error:

"This change set contains components that require the "30.0" or higher platform version."

Thank you for your help!


SwedeBaggieSwedeBaggie
Hi Hobie,

Sounds like your sandbox is on a 14 instance and your live org is on 13. 
If you change the API down to 29 ofor example and try again this should do it.
(Open the class, press edit, version settings Salesforce.com-API)

thanks

Ramakrishnan AyyanarRamakrishnan Ayyanar
Hi Vinita_SFDC,

I found error in your code. please change LIST<Document> to LIST<Attachment> bcoz query from attachment.


Prafull G.Prafull G.
Can we use the same approach to show Attachments related to Account object to Partner Portal and Customer Portal users?
Pintu FrancisPintu Francis

Why don't we use the standard way? We cannot guarentee that Salesforce will keep the URL /servlet/servlet.FileDownload?file= for ever. If this changes evrything will break. 
We have $Action (https://www.salesforce.com/us/developer/docs/pages/Content/pages_variables_global_action.htm) for this purpose.

Use  <apex:image url="{!URLFOR($Action.Attachment.Download, AttachemntId)}" /> instead. The standard way.
 

chaitanya Krishna 19chaitanya Krishna 19
Hi  Puja_mfsi ,,

What if we have list of images and we want to display list of images in vf..
Itzik WinogradItzik Winograd
Hi @Puja_mfsi
Any idia how to implement this in a VF component isntead of a VF page?

Thanks in advanced!
Michaela ChrysostomouMichaela Chrysostomou
Can anyone help me with the Test Class of bellow Apex class?

Public Class displayImageExtension {
    
String recId;          
public displayImageExtension(ApexPages.StandardController controller) {        
 recId = controller.getId();       
}          
public String getFileId() {         
String fileId = '';        
 List<Attachment> attachedFiles = [select Id from Attachment where parentId =:recId order By LastModifiedDate DESC limit 1];         //

if( attachedFiles != null && attachedFiles.size() > 0 ) {            
 fileId = attachedFiles[0].Id;      }         

return fileId;         }
}
anjana varandanianjana varandani
Hello Friend I applied this code and it's work but when I don't wnat to upload any file so show

Subscript is invalid because list is empty
Error is in expression '{!st.Attachments[0].Id==null}' in component <apex:column> in page imageview
 this error and not show records any solution???
SINUHE GARCIA MARTINEZSINUHE GARCIA MARTINEZ
Hello, I need get a Image from CombinedAttachment object and show in a Visualforce page. I tried with <apex:image url="/servlet/servlet.FileDownload?file=file.Id"/> but don't work, and also get CombinedAttachment with a Query but don't work replacing file id with Id from CmbinedAttachment record. Does anyone know how to do it?