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
anukarthi_nimmalaanukarthi_nimmala 

Regarding dispaly blob value

HI,

 

Iam having a soql query whose o/p i pased to a list  of DOcumnt type

 

List<Document> docdisplaylist=new  List<Document> ();
Public List<Document> getdocdisplaylist()
{

   return docdisplaylist;
}

Document d=new Document();
public pageReference docrecord()
{
  try
  {
  d=[Select Document.name,Document.description,Document.body,Document.keywords From Document where Document.id=:docid ];
  docdisplaylist.add(d);
  }

 

Now iam trying to display that list values in a pageblock table as shown below

 

 

 

 <apex:pageBlockTable value="{!docdisplaylist}" var="dd">
 
  <apex:column ><apex:inputField value="{!dd.name}" onchange="updateRecords()"  />                       </apex:column>
    <apex:column ><apex:inputField value="{!dd.description}"   onchange="updateRecords()" /></apex:column>
  <!-- <apex:column ><apex:inputFile value="{!dd.body}" onchange="updaterec()"  /></apex:column>-->

In thie above line iam getting error as below

   <!--<apex:inputField> does not currently support Blob fields, please use <apex:inputFile>-->
 <apex:column ><apex:inputField value="{!dd.keywords}"  onchange="updateRecords()" /></apex:column>
 </apex:pageBlockTable>

So here iam not able to display document body in <apex:inputfield >

 

 

So What should i do now to display the document.body in visualforce  pages pgblock table .Please someone help me in doing it...

 

 

 

                                                                                                 Thanks and Regards,

                                                                                                                 Anu....

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Pradeep_NavatarPradeep_Navatar

Please find below the sample code :

 

VF page:

 

                <apex:inputFile value="{!propFileBody}" fileName="{!propFileName}" contentType="{!propContentType}" id="iFile"></apex:inputFile>

                <apex:commandButton value="update" styleclass="form-submit subclick"  action="{!save}"/>

                <apex:pageBlock> </apex:pageBlock> // rerender this section after save the image in document

 

 

Class:

 

                public transient String propFileName{ get; set; }

    public transient Blob propFileBody{ get; set; }

    public transient String propContentType { get; set;}

                public void save()

                {

                    List<Document> lstDoc = Database.query('SELECT d.Id, d.Name, d.Body, d.ContentType, d.IsPublic, d.FolderId  FROM Document d WHERE d.Name Like \'' + Userinfo.getUserName() + '%\'');

                                if(propFileBody != null && propFileName != null && propContentType != null && propContentType != '')

        {

            if(lstDoc != null && lstDoc.size() > 0 && (lstDoc[0].ContentType == 'image/png' || lstDoc[0].ContentType == 'image/x-png' || lstDoc[0].ContentType == 'image/pjpeg' || lstDoc[0].ContentType == 'image/jpeg' || lstDoc[0].ContentType == 'image/gif' || lstDoc[0].ContentType == 'image/bmp')) //Update Record

            {

                lstDoc[0].Name = Userinfo.getUserName();

                lstDoc[0].Body = propFileBody;

                lstDoc[0].ContentType = propContentType;

                lstDoc[0].Type = propContentType.substring((propContentType.indexOf('/')+1),propContentType.length());

                system.debug('Updated*********');

                Database.SaveResult sr = Database.Update(lstDoc[0]);

            }

            else //New Record

            {

                Document doc = new Document();

                Folder fol = [Select f.Name,f.Id From Folder f where f.Name='Contact Images'];

                doc.Body = propFileBody;

                doc.Name = Userinfo.getUserName();

                doc.IsPublic = true;

                doc.ContentType = propContentType;

                doc.Type = propContentType.substring((propContentType.indexOf('/')+1),propContentType.length());

                doc.FolderId = fol.Id;

                system.debug('Inserted*********');   

                Database.SaveResult sr = Database.Insert(doc);

            }

        }

        propFileBody = null;

 

                }

 

                Rerender Part has done by generic way.

 

                    String strUserNM    = '';

                                String tempUsrPhoto = '';

                                String templogUsrPto = '';

                                String strDocUrl    = '';

                                strOrgId  = UserInfo.getOrganizationId();

                                strDocUrl = '/servlet/servlet.ImageServer?oid=' + strOrgId + '&id=';

                                public String profile_photo_png { get; set; }

                    strUserNM = UserInfo.getUserName();

        String strNm = strUserNM + '%';

        Document[] d = [Select d.Name, d.ContentType From Document d where Name like : strNm limit 1];

        if(d.size() > 0)

        {          

            strUserNm = d[0].Name;

        }

        List<Document> lstDocs = [SELECT d.id, d.name, d.body FROM Document d WHERE d.name IN : lstDocNames OR d.name IN : lstCssFileNames OR d.name =: strUserNm];

        if(lstDocs != null && lstDocs.size() > 0)

        {

            for(Document doc :lstDocs)

            {

                                                    if(doc.Name == strUserNM)

                templogUsrPto = strDocUrl + doc.id;

                                                    else if(doc.name == 'profile-photo')

                tempUsrPhoto = strDocUrl + doc.id;

                                                }

                                }

        if(templogUsrPto != '')

             profile_photo_png = templogUsrPto;

        else

            profile_photo_png = tempUsrPhoto;

 


Did this answer your question? if so, please mark it solved.

 

 

All Answers

NaishadhNaishadh

I don't think you can display document body  in visual force page. You can display it as link and download it.

 

Ref : http://community.salesforce.com/t5/Apex-Code-Development/Displaying-the-content-of-the-Word-Document-in-a-Visualforce/m-p/135839 

Jeremy.NottinghJeremy.Nottingh

Also, if the blob is plain text, you can convert it to a string and use inputtext to display/edit it.

 

Jeremy

Pradeep_NavatarPradeep_Navatar

Since the uploaded file is of type Blob you will not be able to display  the content directly in the VF page. For this first you have to save the uploaded file in Document object and after save rerender the VF page by passing the Document record (the file you have uploaded) URL in the pageBlock you want to display the content.

 

Hope this helps.

anukarthi_nimmalaanukarthi_nimmala

Hi,

 

Thanks for your reply.As you said  we can display it as a link and download.Please give me an example of displaying the documnet body in the form of url and link it to downlod the doccs body..So please GIve an example code  if possible so that it wouldbe helpful to me.

 

                                                                                                                                                                         Thanks and regards,

                                                                                                                                                                                               Anu..

Pradeep_NavatarPradeep_Navatar

Please find below the sample code :

 

VF page:

 

                <apex:inputFile value="{!propFileBody}" fileName="{!propFileName}" contentType="{!propContentType}" id="iFile"></apex:inputFile>

                <apex:commandButton value="update" styleclass="form-submit subclick"  action="{!save}"/>

                <apex:pageBlock> </apex:pageBlock> // rerender this section after save the image in document

 

 

Class:

 

                public transient String propFileName{ get; set; }

    public transient Blob propFileBody{ get; set; }

    public transient String propContentType { get; set;}

                public void save()

                {

                    List<Document> lstDoc = Database.query('SELECT d.Id, d.Name, d.Body, d.ContentType, d.IsPublic, d.FolderId  FROM Document d WHERE d.Name Like \'' + Userinfo.getUserName() + '%\'');

                                if(propFileBody != null && propFileName != null && propContentType != null && propContentType != '')

        {

            if(lstDoc != null && lstDoc.size() > 0 && (lstDoc[0].ContentType == 'image/png' || lstDoc[0].ContentType == 'image/x-png' || lstDoc[0].ContentType == 'image/pjpeg' || lstDoc[0].ContentType == 'image/jpeg' || lstDoc[0].ContentType == 'image/gif' || lstDoc[0].ContentType == 'image/bmp')) //Update Record

            {

                lstDoc[0].Name = Userinfo.getUserName();

                lstDoc[0].Body = propFileBody;

                lstDoc[0].ContentType = propContentType;

                lstDoc[0].Type = propContentType.substring((propContentType.indexOf('/')+1),propContentType.length());

                system.debug('Updated*********');

                Database.SaveResult sr = Database.Update(lstDoc[0]);

            }

            else //New Record

            {

                Document doc = new Document();

                Folder fol = [Select f.Name,f.Id From Folder f where f.Name='Contact Images'];

                doc.Body = propFileBody;

                doc.Name = Userinfo.getUserName();

                doc.IsPublic = true;

                doc.ContentType = propContentType;

                doc.Type = propContentType.substring((propContentType.indexOf('/')+1),propContentType.length());

                doc.FolderId = fol.Id;

                system.debug('Inserted*********');   

                Database.SaveResult sr = Database.Insert(doc);

            }

        }

        propFileBody = null;

 

                }

 

                Rerender Part has done by generic way.

 

                    String strUserNM    = '';

                                String tempUsrPhoto = '';

                                String templogUsrPto = '';

                                String strDocUrl    = '';

                                strOrgId  = UserInfo.getOrganizationId();

                                strDocUrl = '/servlet/servlet.ImageServer?oid=' + strOrgId + '&id=';

                                public String profile_photo_png { get; set; }

                    strUserNM = UserInfo.getUserName();

        String strNm = strUserNM + '%';

        Document[] d = [Select d.Name, d.ContentType From Document d where Name like : strNm limit 1];

        if(d.size() > 0)

        {          

            strUserNm = d[0].Name;

        }

        List<Document> lstDocs = [SELECT d.id, d.name, d.body FROM Document d WHERE d.name IN : lstDocNames OR d.name IN : lstCssFileNames OR d.name =: strUserNm];

        if(lstDocs != null && lstDocs.size() > 0)

        {

            for(Document doc :lstDocs)

            {

                                                    if(doc.Name == strUserNM)

                templogUsrPto = strDocUrl + doc.id;

                                                    else if(doc.name == 'profile-photo')

                tempUsrPhoto = strDocUrl + doc.id;

                                                }

                                }

        if(templogUsrPto != '')

             profile_photo_png = templogUsrPto;

        else

            profile_photo_png = tempUsrPhoto;

 


Did this answer your question? if so, please mark it solved.

 

 

This was selected as the best answer