2

I am new to Salesforce.

We have a custom object, which has one field of type ReachTextArea. This field is basically used for storing image. When we do a query on that field from Workbench, instead of getting the image as byte format, we are getting an URL, for eample: https://c.cs17.content.force.com/servlet/rtaImage?eid=a0Qg0000009X4qV&feoid=00Ng0000001wxcM&refid=0EMg0000000gz80

Once I do an authentication, I am able to access this URL from browser. After going through little web search: rtaimage servlet parameters...what does the refid refer to? I understood, specification for each of the field in that URL.

But my query is:

  1. How programatically, can I read that image and store it in my local disk (through REST API- https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_authentication.htm)

  2. How can I insert that image later from my local driver to that HTML field programatically? the complexity will be, how to get the "refid=" value?

Any code example will be helpful.

1

For Question #1 I tried the following approach and it worked for me.

Create a REST Service. First get the Binary Image data into a Blob and pass it as Base64 encoded String as service response.

Now you can consume the Webservice from any client including Java to get the Base64 String. Decode the Base64 String to get back the Image. In Java you can use FileOutputStream to write the Image file to your disk.

For testing the Base64 String I used this online tool - Convert Your Base64 To Image

Rest Service

@RestResource(urlMapping='/ImageService')
global with sharing class RESTImageController {
    @HttpGet
    global static String getImage(){
        String strBase64 = 'Hello';
        Book__c book = [SELECT Rich_Text__c FROM Book__c WHERE Id = 'a01i000000dwOIb' LIMIT 1];
        String bookimg = book.Rich_Text__c.substringBetween('<img', 'img>');
        String imgsrc = bookimg.substringBetween('src="', '"');
        imgsrc = imgsrc.replace('amp;', '');
        PageReference page = new PageReference(imgsrc);
        Blob imgblob = page.getContent();
        strBase64  = EncodingUtil.base64Encode(imgblob);
        return strBase64;
    }
}

For Question #2 Please refer to this link, first you have to encode the image to a Base64 String, create img element <img alt="<image_Name>" src="data:image/<image_File_Extension>;base64, <base64_Image_String>"></img> and copy the entire source text data and use it as the value in the relevant field of the API.

Extracts from the link.

Uploading Images Using the API If records include rich text area fields containing images, you must convert the image to Base64 and supply that as the value in the relevant field. You can use a text editor to do this.

Solution

1 - Encode the desired image into Base64.

2 - Copy the entire Base64 image string to the clipboard.

3- Copy the following text and paste it into a text editor:

<img alt="<image_Name>" src="data:image/<image_File_Extension>;base64, <base64_Image_String>"></img>

Replace with the file name of the image. Don’t include the file extension. For example: Headshot1 Replace with the extension of the image file. Don’t include the dot. For example: jpg Replace with the string that you copied in Step 2.

4 - Copy the entire source text data and use it as the value in the relevant field of the API.

5 - Upload the data.

  • For #1 if I will find something I will let you know. – Saroj Bera Feb 6 '17 at 12:43
  • Hi Saroj, Thanks for this input. #2: I was able to succesfully insert an image from into RTA type field with the above link. Just two correction: 1. There is a space between ";base64," and "<base64_Image_String>". Once I removed that, it worked fine. 2. Even though my image file(test.JPG) extension is .JPG, when i replace "<image_File_Extension>" with "jpg", Salesforce is throwing exception like not supported mime type. Once I change the extension to "jpeg", it worked fine. <img alt="testImg" src="data:image/jpeg;base64,<base64_Image_String>"></img>" #1: I am still waiting. – Prasenjit Feb 8 '17 at 8:58
  • @Prasenjit just curios from where you are invoking the REST API? – Saroj Bera Feb 8 '17 at 14:55
  • I am following the steps given by this link asagarwal.com/2401/… – Prasenjit Feb 8 '17 at 18:47
  • After creating connected app in SFDC, I am able to access any standard object or custom object. But when I tried to run(with proper Authorization token) c.cs17.content.force.com/servlet/rtaImage for the image, its returning me a response as login page redirect url. – Prasenjit Feb 8 '17 at 18:56
0

I think this is possible, but implementation will vary depending on which language you are using to access the API. This was not specified by the OP, so the approach that is laid out here is language agnostic.

  1. Send a GET request to frontdoor.jsp with a session id and capture all cookies. The request will be redirected a few times, so make sure to follow any redirects.
  2. Using cookies captured from step 1, make subsequent GET requests for images.

A more concrete example in .NET would be to use HttpWebRequest.CookieContainer to capture cookies. Then use the same cookie container on subsequent requests to fetch the images. Not all session ids support this approach (if using OAuth, you may need the web scope for example).

But I don't see any reason why this wouldn't work. You are basically automating a browser in code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.