1

We are having pdf documents on Library objects under shared content.Now we are integrated through html for retriving the data.I Developed Rest class as like below.If i run this i am able to get pdf on browser but pdf was not decoding its giving "FAILED TO LOAD PDF CONTENT".It is an urgent requirement for me can any one suggest to achieve this?

@RestResource(urlMapping='/ContentDoc/*')
global class ContentDocController {
    @HttpGet
    global static void getBlob() {
    RestRequest req = RestContext.request;
    String Id= RestContext.request.params.get('ContentDocId') ;
    ContentVersion a = [SELECT ContentModifiedById,ContentDocumentId,FileExtension,FileType,ContentSize,ContentUrl,Id,VersionData,VersionNumber FROM ContentVersion where ContentDocumentId=:Id];
    RestResponse res = RestContext.response;
    res.addHeader('Content-Type',a.FileExtension);
   //String paramvalue =EncodingUtil.base64Encode(a.VersionData);
    String afterblob = EncodingUtil.base64Encode(a.VersionData);
    res.responseBody =EncodingUtil.base64Decode(afterblob); 
    } 
}

Thanksin advance

3
  • after removing this lin it should work String afterblob = EncodingUtil.base64Encode(a.VersionData);
    – Himanshu
    Jan 31, 2016 at 14:34
  • Hi Himanshu thanks for your reply .I tried but Its giving variable not exist.Can u please guide me how we can modify it
    – Amar
    Jan 31, 2016 at 16:53
  • How do we call this 'getBlob' from client side ? Apr 10, 2018 at 13:12

3 Answers 3

0

I found the solution. we need to mention the 'application/pdf' on the rest class.Then it should load the pdf.

@RestResource(urlMapping='/ContentDoc/*')
   global class ContentDocController {
    @HttpGet
    global static void getBlob() {
    RestRequest req = RestContext.request;
    String Id= RestContext.request.params.get('ContentDocId') ;
    ContentVersion a = [SELECT ContentModifiedById,ContentDocumentId,FileExtension,FileType,ContentSize,ContentUrl,Id,VersionData,VersionNumber FROM ContentVersion where ContentDocumentId=:Id];
    RestResponse res = RestContext.response;
    res.addHeader('Content-Type',a.FileExtension);
     res.addHeader('Content-Type','application/pdf');
    String afterblob = EncodingUtil.base64Encode(a.VersionData);
    res.responseBody =EncodingUtil.base64Decode(afterblob); 
    } 
}
1

The Versiondata field gives the base 64 encoded type data.In Your code you are encoding it again ,I think that's causing problem.Try Just decoding it.It will return you a blob and then try it.

1
0

It might work for you

@RestResource(urlMapping='/ContentDoc/*')
global class ContentDocController {
    @HttpGet
    global static void getBlob() {
    RestRequest req = RestContext.request;
    String Id= RestContext.request.params.get('ContentDocId') ;
    ContentVersion a = [SELECT ContentModifiedById,ContentDocumentId,FileExtension,FileType,ContentSize,ContentUrl,Id,VersionData,VersionNumber FROM ContentVersion where ContentDocumentId=:Id];
    RestResponse res = RestContext.response;
    res.addHeader('Content-Type',a.FileExtension);
    res.responseBody =a.VersionData; 
    } 
}
3
  • HI Himanshu it is giving Error: Compile Error: Method does not exist or incorrect signature: EncodingUtil.base64Decode(Blob) at line 10 column 23
    – Amar
    Jan 31, 2016 at 17:06
  • @himanshu. Also include in the where clause 'isLatest = true'
    – cropredy
    Feb 1, 2016 at 1:58
  • Thanks for giving reply.still it is giving error as ![enter image description here Any other way please suggest me Feb 1, 2016 at 18:54

You must log in to answer this question.

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