Lightning Aura CORS Blocked using JSZip library

corsjavascriptlightning-aura-components

I have files on a sharepoint server which I want my lightning aura component to get and download for me. I am using JSZip to zip the files together.

As my download function requests the files from the web URL of the files from sharepoint, I get the following error:

Access to XMLHttpRequest at [Sharepoint URL] from origin 'https://nec--testing.lightning.force.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Is the error being given by the sharepoint server or by lightning? If by sharepoint, is my only option to somehow make those web URLs have a Access-Control-Allow-Origin header? (is that even possible?) If by lightning, what should I do?

I would appreciate any explanation on what is going on. Thanks.

Best Answer

This is an error caused by SharePoint not allowing access (Access-Control-Allow-Origin), the error is provided by the web browser. This can't be fixed from the Lightning side of things.

I won't provide any specific links, as I can't seem to find a reliable (e.g. official Microsoft documentation) on the matter, but it seems you might be able to convince SharePoint to add the right headers; I'd recommend that you do your own research, contact their Support, and/or ask on Stack Overflow for a better chance of solving the problem.

However, if the files are small (<3MB or so), you could potentially write some Apex code to call the SharePoint server, or if that doesn't work, you could also potentially spin up a Heroku app or other type of server you can control to act as a proxy. Servers aren't bound by CORS and can therefore do whatever connections they want. There's likely a viable solution lurking out there, but it's either not straight-forward, or not well-documented.

Related Topic