Section:

Opening a document from the Salesforce Database

If you have a document uploaded into your instance of Salesforce, it will have a record in the database in the ContentVersion table. The ContentVersion object allows for versioning of the ContentDocument which is linked to a Salesforce record via the ContentDocumentLink.

For Salesforce files smaller than 25 MB, you can use this method to retrieve and display them in WebViewer.

Process Flow

small file process flow

ContentVersionController.cls

Apex

1// force-app\main\default\classes\Apryse_ContentVersionController.cls
2
3//snipped for brevity
4@AuraEnabled(Cacheable=true)
5public static ContentVersionWrapper getFileDataFromId(String Id) {
6 try {
7 Long max = 1024 * 1024 * 25; // 25 MB
8 ContentVersion cv = [SELECT Title, FileExtension, ContentSize, VersionData, ContentDocumentId, LastModifiedDate FROM ContentVersion WHERE Id = :Id];
9 if(cv.ContentSize > max) {
10 throw new ApryseException('Your file size must not exceed ' + convertBytesToFormattedString(max) + ' - current file size: ' + convertBytesToFormattedString(cv.ContentSize));
11 }
12 return new ContentVersionWrapper(cv);
13 } catch(Exception e) {
14 throw new AuraHandledException(e.getMessage());
15 }
16}
17
18//sample wrapper class
19public class ContentVersionWrapper {
20 @AuraEnabled
21 public String name { get; set; }
22
23 @AuraEnabled
24 public String body { get; set; }
25
26 @AuraEnabled
27 public ContentVersion cv { get; set; }
28
29 public ContentVersionWrapper(ContentVersion contentVer) {
30 this.cv = contentVer;
31 this.name = contentVer.Title + '.' + contentVer.FileExtension;
32 this.body = EncodingUtil.base64Encode(contentVer.VersionData);
33 }
34}

ApryseWvInstance.js

HTML

1// force-app/main/default/lwc/apryseWvInstance/ApryseWvInstance.js
2
3//snipped for brevity
4import { registerListener, unregisterAllListeners } from 'c/pubsub';
5
6connectedCallback() {
7 registerListener('blobSelected', this.handleBlobSelected, this);
8 //register other listeners here
9}
10
11disconnectedCallback() {
12 //unregister all listeners here
13}
14
15handleBlobSelected(record) {
16 let blobby = new Blob([_base64ToArrayBuffer(record.body)], {
17 type: mimeTypes[record.cv.FileExtension]
18 });
19
20 const payload = {
21 blob: blobby,
22 extension: record.cv.FileExtension,
23 filename: record.name,
24 documentId: record.cv.Id
25 };
26
27 this.iframeWindow.postMessage({type: 'OPEN_DOCUMENT_BLOB', payload} , '*');
28}

config_apex.js

JavaScript

1//snipped for brevity
2window.addEventListener("message", receiveMessage, false);
3
4function receiveMessage(event) {
5 if (event.isTrusted && typeof event.data === 'object') {
6 switch (event.data.type) {
7 case 'OPEN_DOCUMENT_BLOB':
8 const { blob, extension, filename, documentId } = event.data.payload;
9 instance.loadDocument(blob, { extension, filename, documentId })
10 break;
11 default:
12 break;
13 }
14 }
15}

Converting a base64 string to blob inside your WebViewer LWC

JavaScript

1function _base64ToArrayBuffer(base64) {
2 var binary_string = window.atob(base64);
3 var len = binary_string.length;
4 var bytes = new Uint8Array( len );
5 for (var i = 0; i < len; i++) {
6 bytes[i] = binary_string.charCodeAt(i);
7 }
8 return bytes.buffer;
9}

Code sample

You can find a full code sample that implements opening documents in Salesforce on our Github repository.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales