0

When I'm trying to access a pdf file from the files tab, I'm running into the below issue.

Message: Cross-origin redirection to https://name-space--c.documentforce.com/sfc/servlet.shepherd/document/download/0692I000009ZWwuQAG denied by Cross-Origin Resource Sharing policy: Origin https://name-space.lightning.force.com is not allowed by Access-Control-Allow-Origin.

If I directly give this in the browser: https://name-space--c.documentforce.com/sfc/servlet.shepherd/document/download/0692I000009ZWwuQAG it is downloading the file.

I've added the below two to the CORS (in setup):

https://name-space--c.documentforce.com

https://name-space.lightning.force.com

anything else I'm missing, please suggest.

Thanks

Update

I'm trying to load a PDF in a container .. so that user can view/read on the page itself ... something like PDF file download in Lightning (using mozilla pdf library)

my code

<iframe src="{!$Resource.PDFViewer + '/web/viewer.html?file=/sfc/servlet.shepherd/document/download/0692I000009ZWwuQAG' }">PDF Loading...</iframe>
1
  • as per your recent update, you want to build a lightning component that helps user to read/view a pdf?
    – 7'7'
    Commented Mar 19, 2019 at 12:05

2 Answers 2

2

Usually when you try to access any file form Files tab you will access the ContentDocument ID with URL something like this

https://sample-dev-ed.lightning.force.com/lightning/r/ContentDocument/0696F000009AAAAAAA/view

You will only use the above url you specified can be modified with content version id so that when you want to preview a file which is not supported by Salesforce for default preview like video files etc. can be previewed

Also, the url you are trying to access from Lightning belongs to Documents in classic. So you will not be able to find the file which is uploaded in documents in Lightning Files.

May I know what exactly are you trying to achieve with the URL specified

https://name-space--c.documentforce.com/sfc/servlet.shepherd/document/download/0692I000009ZWwuQAG ??

3
  • please see my update, thanks. Commented Mar 19, 2019 at 12:03
  • as per your recent update, you want to build a lightning component that helps user to read/view a pdf?
    – 7'7'
    Commented Mar 19, 2019 at 12:05
  • Yes, exactly... Commented Mar 19, 2019 at 12:07
2

So as per your updated question and requirement of lightning component to view/load/download any kind of file in browser then you should get respective Content Version of the file you wish to view/download and form a URL.

If it is coming from an sobject record then get the contentDocumentId from the contentDocumentLink record whose linkedEntityId will be recordID. After you get ContentDocumentID, get the latest ContentVersion or number of versions as per requirement and form the URL like this

https://yourDomain.lightning.force.com/sfc/servlet.shepherd/version/download/ContentVersionId

If you want the file to be executed/viewed in browser go to File Upload and Download Security and choose that particular type of file and set the behavior. enter image description here

UPDATE-- You cannot use a URL of

name-space.visualforce.com

in a lightning component. If you wish to preview/download a file uploaded in documents(classic), get that file in lightning either by uploading it manually(if single file or 2) in files tab or a customised code to convert attachment to contentDocument and Version and then get it's ID to use in Lightning component

You can only form the URL i mentioned

https://yourDomain.lightning.force.com/sfc/servlet.shepherd/version/download/ContentVersionId

which has lightning.force.com in your lightning component along with Content version id to preview/view/download following the above procedure.

3
  • still no luck ... I'm running into - Message: Origin name-space.visualforce.com is not allowed by Access-Control-Allow-Origin. Commented Mar 20, 2019 at 7:16
  • I think all these issues are because of the way the pdf is being download in mozilla.pdf.js ... Commented Mar 20, 2019 at 9:00
  • mistake could never be from a file. As i said you are trying to preview file in browser with the help of lightning component without the contentVersion ID which is necessary for the preview. where as you are trying to preview a file from 'Documents in classic' in a lightning component which is wrong. Try to upload a sample file in lightning files and get it's ContentVersion id and check with the URL i have given
    – 7'7'
    Commented Mar 20, 2019 at 9:02

You must log in to answer this question.

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