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
Alok_NagarroAlok_Nagarro 

Render Blob content on the page

Hi,

 

My requirement is to display the blob content on the page, in fact i am making a callout to external webservice and getting a file content in Base64 string format (now we can covert it to Blob). But not able to display this content in correct format, as i tried with different things like -

 

1. converted Base64 in Blob and tried to display but it's displaying something "core.filemanager.FileBlobValue@bd9997c " on the page

2. Directly tried to display Base64 string then obviously it's displaying raw string (very large)

etc.

 

I want to display that content in original format, it can be of any type (pdf, xls, doc,rtf etc).

 

Any help would be appreciate.

 

sfdcfoxsfdcfox

You need a way to deliver the BLOB to the client so it can render it. Base64 would work if you used a DATA URL, but depending on the browser, content might not fit in the DATA URL limit (IE6 supports 4096 characters, for example). Without knowing the content type in advance, you would need to render the data inside an IFRAME as an additional inconvenience, as HTML doesn't just render any type of file without being told what to do with it. For example, a PDF needs an IFRAME, while an IMAGE needs a IMG tag, etc. If you limit yourself to a select list of file types, you could probably get away with conditional rendering of certain types, and provide an IFRAME for all remaining types that HTML doesn't have distinct types for. I think your functionality may be too generic to provide any solution other than "allow the user to download the file directly to their computer."

Alok_NagarroAlok_Nagarro

Hi,

 

Thanks for valuable reply.

 

I am getting the file content from external file server using webservice callout also getting the content type of file, now is there any way to render the content on page..?

 

I want to have same functionality as in salesforce we upload a file in document object and when we click on "view File"  link, it render the file if file is of type text/image/pdf otherwise it gives the option to dowload/save file.

 

Temporarily i am doing same, just getting content from callout..., saving the file content in document object and finally redirecting user to that (View File) link. But the prob is that we dont want to save file in salesforce.

 

As your reply, can we implement this using IFRAME ? if so then in which format(Base64/Blob/String..) we need to pass the file content in IFRAME. A sample example will help me more if you could..

sfdcfoxsfdcfox

You'll have to set the Content-Type to "application/octet-stream", and then you'll be at the mercy of the browser to determine the type of file and how to display it. I've never tried this before personally, but I think it should work on most browsers without a problem. However, you may have to implement this as a REST method in order to set the correct headers.

Senthil.GSenthil.G

This knowledge article might help you,

 

http://help.salesforce.com/apex/HTViewSolution?id=000005941&language=en_US

 

Description

How can I Display Base64 Data on a Visual force page?




Resolution

<apex:page controller="ViewImage" cache="true">

      <img src="data:{!att.ContentType};base64,{!image}" /> </apex:page>

 

public class ViewImage {

      public Attachment att {

            get {

                  if (att == null) {

                        String id = ApexPages.currentPage().getParameters().get('AttachmentID');

                        att = [SELECT Body, ContentType, Name FROM Attachment WHERE ID = :id];

                  }

                  return att;

            }

            private set;

      }

      public String image {

            get {

                  return EncodingUtil.Base64Encode(att.body);

            }

      }

}

Tejpal KumawatTejpal Kumawat

Hello Alok,

 

Have you find any alternative for this requirement? If Any please suggest me..

justin_sfdcjustin_sfdc
Hi there,

I am currently stuck at the same issue. Have you been able to resolve yours. Please suggest me the same?
badibadi
Check the response headers for "Content-Disposition" which will be like 
Content-Disposition -> attachment; filename=yourfile.ext
Controller Code:
HTTPResponse res = http.send(request);
Blob jsonresponse=res.getBodyAsBlob();
List<String> resHeaderKeys=res.getHeaderKeys();
for(Integer i=0; i<resHeaderKeys.size(); i++){
   if(resHeaderKeys[i] == 'Content-Disposition'){
     System.debug('The value of the Content Disposition '+res.getHeader(resHeaderKeys[i]));
   }
}
String fileString=EncodingUtil.Base64Encode(jsonresponse);
Parse the header value for the Content-Disposition and get the filename. Based on the file extension you can change the MIME Type
if(fileName.endsWith('.pdf')){
            conType='application/pdf';
        }else if(filename.endsWith('.xls')){
            conType='application/vnd.ms-excel';
        }else if(fileName.endsWith('.xlsx')){
            conType='application/vnd.ms-excel';
        }

etc...
you can use that to display/ force download the file in Visualforce page 
 
<!-- PDF Download code
    <iframe src="data:application/pdf;base64,{!fileString}" style="width:100%;" id="frame" frameborder="0"></iframe>
    -->
    <!-- Excel - .xlsx code
    <iframe src="data:application/vnd.ms-excel;base64,{!fileString}" style="width:100%;" id="frame" frameborder="0"></iframe>
    -->

Hope this helps

 
Sarah J DSarah J D
Hi Alok_Nagarro,

Kindly share if you find any solution for the issue posted? Same issue I'm also facing.

Appreciate any solutions!

Regards
Sarah