+ Start a Discussion
Arpit Jain92Arpit Jain92 

File preview is not working in community with LWC component

Hello Everyone,

I am using an lwc component on which users will upload a file and they can also preview that. For a preview, I am using navigationMixin and navigate the record to standard_namedPage which working fine for internal use but the same method is not working in the community. I am not able to preview the file using that. Today I did some research and get to know standard_namedPage not supported community and for that, I need to use standard_webPage which redirects me to the new tab and I can see my file there but the issue is on that new tab I can see my file as a thumbnail, not the actual file. Can someone guide me on how can I preview files in the community as the original file?
Below is the code that I am using for both internal and external.
type: 'standard__namedPage',
attributes: {
pageName: 'filePreview'
state : {
// assigning ContentDocumentId to show the preview of file

//Getting content version id to assign the url.
contentVersionId = response;
var baseURL = 'https://'+location.host+'/';
var previewURL = baseURL + 'sfc/servlet.shepherd/version/renditionDownload?rendition=THUMB720BY480&versionId='+contentVersionId;
type: 'standard__webPage',
attributes: {
url: previewURL
}, false );

Arpit Jain
AbhishekAbhishek (Salesforce Developers) 

Try the code snippet as mentioned in the above blog.

For further reference check the below,

I hope it helps.
I'm stuck with the same issue. I want to preview a file in my community. I followed the mentioned article from sfdcpanther, but it always opens a new tab with a thumbnail. I want it to behave like known from within lightning. It should open this preview overlay on the page. Was anyone already able to achieve this?
Arpit Jain92Arpit Jain92
Still didn't get any update. So, later I use Aura component for that functionality.
Sumit Kumar 10Sumit Kumar 10
Hi @Arpit Jain92,
I am facing this issue in Salesforce site. Preview is not working. Can you help me with that?
Arpit Jain92Arpit Jain92

For LWC I didn't get any solution but for Aura component you can show preview on community.
For You can refer below code:

Component Code:

<aura:iteration items="{!v.SignedGADPackage}" var="item" indexVar="index">
<td width="25%">
<td width="25%">
<lightning:formattedDateTime value="{!item.generatedDocTime}" year="numeric"
month="numeric" day="numeric" hour="2-digit" minute="2-digit"
hour12="true" />
<td width="25%">
<td width="25%">
<a href="" id="{!item.conDocId}" onclick="{!c.previewFile}">View&nbsp;
<lightning:icon size="x-small" iconName="action:preview" alternativeText="Preview"
title="Preview" />
<a href="javascript:void(0)" id="{!item.conDocId}"
<lightning:icon size="x-small" iconName="action:download" alternativeText="Download"
title="Download" />

JS Code:

previewFile: function (component, event, helper) {
var rec_id = event.currentTarget.id;
recordIds: [rec_id]

downloadFiles : function (component, event, helper)
var documentId = event.currentTarget.id;
var urlString = window.location.href;
var baseURL = urlString.substring(0, urlString.indexOf("/s"));
var urlEvent = $A.get("e.force:navigateToURL");
"url": baseURL + '/sfc/servlet.shepherd/document/download/' + documentId + '?operationContext=S1'

Please markit as best answer if this helps you.


Arghyadeep SurArghyadeep Sur
Hi @Arpit ,

You can use the following codes for the purpose-

let baseUrl=your portal url+'/s/contentdocument/'+file.ContentDocumentId;
window.open(baseUrl);  //to open in new tab
//to open in same tab
let baseUrl=your portal url+'/s/contentdocument/'+file.ContentDocumentId;
            type: 'standard__webPage',
            attributes: {
                url: baseUrl
        }, false );

Your portal URL can be found by navigating to Feature Settings-->Digital Experiences-->All Sites.

Please give this a thumbs up if this helps you.

Thanks and regards,
Tushar RashinkarTushar Rashinkar

I build one community site .I was showing file priview by your approach.It was working fine for 'Internal' users.But priview was giving error for community license users.
Below workaround worked for me
1) Write one trigger on contentVersion .In 'After insert ' trigger ,Create corresponding record in 'ContentDistribution' object,

2) PdfDownloadUrl and DistributionPublicUrl fields on contentDistribution gets populated automatically.

3) Query this object and show file preview using this links.