You need to sign in to do that
Don't have an account?
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>
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
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.
<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 :)