You need to sign in to do that
Don't have an account?

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.
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
Hello,
Please refer the following example :
Controller :
=================================
=====================
Hi Vinita_SFDC,
Thanks. I want to display the image dynamically from attachments. If there are three images in attachments, i want to display the latest image from attachment according to LastModifiedDate in detail page of custom object. I will use embedded visualforce in page layout for display image in detail page of custom object.
Before that i have get the image in vf page. I'm trying for that but could not able to display the image. Any idea about this????
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 .
Really thanks... It is working fine. I can see the image in detail page of my object. Once again thanks.
How to display list of images on visualforce page from attachments/Documents?
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!
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
I found error in your code. please change LIST<Document> to LIST<Attachment> bcoz query from attachment.
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.
What if we have list of images and we want to display list of images in vf..
Any idia how to implement this in a VF component isntead of a VF page?
Thanks in advanced!
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; }
}
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???