0

Target Org Code

@RestResource(urlMapping='/getContentVersions/*')
global with sharing class ShareFileRest {

@HttpPost
global static String getFileToStore() {
    RestRequest req = RestContext.request;
    RestResponse res = RestContext.response;
    system.debug('request'+req); 
      String name = req.requestURI.substring(
                    req.requestURI.lastIndexOf('/')+1);

    //Insert opportunity
    Opportunity opp = new Opportunity(Name='Test Opportunity');
    opp.StageName='closed';
    opp.CloseDate=system.today();
    opp.SOP_date__c=system.today();
    Insert opp;

   //insert contentVersion
    contentVersion cVersion = new contentVersion();
    cVersion.Title = name;
    system.debug('title'+cVersion.title);
    cVersion.PathOnClient = name +'.'+'jpg'; 
    system.debug('PathOnClient'+cVersion.PathOnClient);
    cVersion.versionData =req.requestBody;
    system.debug('Version data'+cVersion.versionData);
    insert cVersion; 
     
    ID conDocument = [Select ContentDocumentID from ContentVersion 
    where ID=:cVersion.ID].ContentDocumentID;
   //insert ContentDocumentLink
 
    ContentDocumentLink conDocLink = new ContentDocumentLink();
    conDocLink.LinkedEntityId = opp.Id;
    conDocLink.ContentDocumentId = conDocument;
    conDocLink.shareType = 'V';
    conDocLink.Visibility = 'AllUsers';
    insert conDocLink; 

     String downUrl;
    String url = URL.getSalesforceBaseUrl().toexternalForm(); //Returns a string representation of the current URL.
    String substring = '.lightning.force.com';
    downUrl= 'https://ds-dev-ed.lightning.force.com'; //  - It is use for get index of string/character from the specified from index.
   return ( downUrl +'/lightning/r/Opportunity/'+cVersion.Id+'/view ');    

    
} 

}

enter image description here enter image description here

0

1 Answer 1

0

You DO NOT need the conversion to a string & then blob again. Since the request body already has it as a BLOB, you can directly put it in the body.

    Content version cv = new ContentVersion(
       Title = fileName,
       PathOnClient = '/' + fileName,
       VersionData = req.requestBody
    );

Also make sure the filename has the extension (.TXT, .jpg, .pdf etc) as well & you're putting it correctly in the request body. If it is done in apex you can use req.setBodyAsBlob(<body of the file as blob>);

UPDATE: You can not put the request body like that, since you doing concatenation with a blob value.

  • The target org would not longer get it as blob, instead it would be a string.
  • Since you're also passing other parameters it can't be setBodyAsBlob directly.

The code in the source should be something like,

Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(<target org endpoint>);
req.setMethod('POST');
req.setBody(
    JSON.serialize(new map<String, String>{
        'Title' => 'filename.jpg',
        'VersionData' => EncodingUtil.base64Encode(conDoc.VersionData),
        'FileExtension' => '.jpg',
    })
);
HttpResponse res = http.send(req);

And you can get contents in the target org something like this map<String, Object> requestMap = (map<String, Object>)JSON.desializeUntyped(req.requestBody.toString());

ContentVersion cv = new ContentVersion(
    VersionData = EncodingUtil.base64Decode((String)requestMap.get('VersionData')),
    Title = (String)requestMap.get('Title'),
    PathOnClient = '/' + (String)requestMap.get('Title')
);

& insert it their.

And it wouldn't work for larger files(6/12MB for sync/async) since it will blow transaction heap limits. The alternative could be the one mentioned here. See this link as well.

5
  • Thank u ` //insert contentVersion contentVersion cVersion = new contentVersion(); cVersion.Title = name; cVersion.PathOnClient = name +'.'+'jpg'; cVersion.versionData =req.requestBody; insert cVersion; ` still im not able to get the preview
    – user102524
    Aug 11, 2021 at 5:36
  • Can you please add the code where you're invoking this REST service? Aug 11, 2021 at 5:38
  • I have Updated the code
    – user102524
    Aug 11, 2021 at 5:50
  • Please try with the updated code. Aug 11, 2021 at 7:29
  • 1
    Thank u so much for your time... It's Worked :)
    – user102524
    Aug 11, 2021 at 8:12

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