1

I have preview page and pass Id in url its show break image not showing proper image this is my class code

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;    
    }
}

this is my page code

<apex:page standardController="Account" extensions="displayImageExtension">
    <apex:form>
        <apex:image url="{!URLFOR($Action.Attachment.Download,FileId" />
    </apex:form>
<apex:page>
3
  • While moving your code out of the image and into text, I noticed a syntax error- url="{!URLFOR($Action.Attachment.Download,FileId" should be url="{!URLFOR($Action.Attachment.Download,FileId)}". You may want to try resolving this issue. Jul 5, 2016 at 13:29
  • i tried then its shows Invalid parameter for function URLFOR Jul 5, 2016 at 13:40
  • Can you edit the question to include the URLFOR you are currently using? Jul 5, 2016 at 14:03

1 Answer 1

1

You can show any image preview on Visualforce page

Check this code

Page

<apex:page controller="ViewPdf" >
    <iframe height="250px" width="350px" src="data:{!att.ContentType};base64,{!blobData}" ></iframe>
</apex:page>

Controller

public class ViewPdf {
    public Document att {
        get {
            if (att == null) {
                att = [SELECT Id,Body, ContentType, Name FROM Document Limit 1];
            }
            return att;
        }
        private set;
    }
    public String blobData {
        get {
            return EncodingUtil.Base64Encode(att.body);
        }
    }
}
  1. Using this code you can show all text files, images and PDFs on VF page.
  2. Type of the document will be determined dynamically.
  3. Support for other documents depends on the browser.
3
  • my requirment is Without Iframe show image on vfpage Jul 5, 2016 at 13:27
  • You can also use html image tag in place of iframe Jul 5, 2016 at 13:29
  • use <img id="theImage" src="data:{!att.ContentType};base64,{!blobData}"/> Jul 5, 2016 at 13:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .