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
Suresh440Suresh440 

View Notes And Attachments in Visualforce Page

Hi All,

 

Iam trying to obtain or view the notes and Attachments Document of an object  in a visualforce Page if the Notes&Attachment file is in Text format

 

Thanks In advance

MiddhaMiddha

You can query the Attachment Object and show the Body value as follows:

 

List<Attachment> atts = [Select Id, Body from Attachment where name='abc.txt' limit 1];

System.debug('\n\nBODY: ' + atts[0].Body.toString());

 This works only for text files, you cannot get contents for other content types.

Itachi-sanItachi-san

Good morning, 

 

thanks for your response, it was very helpful, but is there anyway to read audio files iin attachment,

I'm trying this:

 

<apex:page controller="ViewAudio" cache="true">
<audio controls="controls">
<source src="{!audio}" type="{!att.ContentType}" />
<embed src="{!audio}" />
</audio>
</apex:page>

 

and here is the controller code

 

public class ViewAudio {
   public Attachment att {
      get {
         if (att == null) {
         // the parent id is a place holder, i'm just testing it with a specified value
         att = [SELECT Body, ContentType, Name FROM Attachment WHERE PARENTID = <parentid>];
         }
      return att;
      }
      private set;
   }
   public String audio {
      get {
         return EncodingUtil.Base64Encode(att.body);
      }
   }
}