Lightning Components – Implementing Document Previewer Functionality

I don't know whether this is possible or not. I have a requirement to preview all the documents(ContentDocument) related to a particular record in preview mode in my lightning component. Till now I was using lightning:carouselImage, which is restricted to only images, but the document can be of any type, i.e, a PDF, a word document, or an image. Is there any way such a thing can be accomplished via a lightning component, so basically a user can easily browse through in preview mode, all the documents related to a record.

Thanks in advance!

Best Answer

You can use lightning:openFiles to allow a preview of files of supported types, including popular office file formats. This works for ContentDocument and ContentHubItem record Ids, but not Attachments or Documents. First, query the record values, then fire the event.

Here's the example from the documentation:

 ({
    openSingleFile: function(cmp, event, helper) {
        $A.get('e.lightning:openFiles').fire({
            recordIds: ['fileid0']
        });
    }, 
    openMultipleFiles: function(cmp, event, helper) {
        $A.get('e.lightning:openFiles').fire({
            recordIds: ['fileid0', 'fileid1', 'fileid2'], 
            selectedRecordId: 'fileid1'
        });
    }, 
    handleOpenFiles: function(cmp, event, helper) {
        alert('Opening files: ' + event.getParam('recordIds').join(', ') 
            + ', selected file is ' + event.getParam('selectedRecordId')); 
    }
})

Please note that this event requires a Lightning Experience environment, so it will not work in standalone Lightning Out or Lightning Apps.

Related Topic