We are trying to build a zip download feature for documents. The zip can contain files stored in ContentVersions and external files.

It works fine for external files.

But due to CORS, we cannot access the normal download servlet /sfc/servlet.shepherd/version/download/ from lightning JS since that redirects to files.force.com and the CORS restrictions don't allow accessing the data from there. I added file.force.com to trusted URLs to fix the CSP error, but the CORS error remains.

So I built a small Apex function that should return the file content:

public static Blob getFileContents(String docId) {
    try {
      ContentVersion conVer = [
       SELECT Id, VersionData
        FROM ContentVersion
        WHERE (Id = :docId OR ContentDocumentId = :docId) AND isLatest = TRUE
      return conVer.VersionData;
    } catch (Exception e) {
      throw new AuraHandledException(e.getMessage());

but when I call that from lightning js, I get a 500 response and no log. The 500 also happens when called from debug console.

Salesforce support says finding the source of the 500 would need developer support and thats not covered by the standard support plan.

So is there any other on-platform way to get the file contents of a contentDocument by lightning code?

I am aware that If I were using FilesConnect I could use the shepherd servlet for zip download but that is not feasible for other reasons.

1 Answer 1


You can't return binary data directly. You have to serialize it first somehow. One of the most efficient ways to do so is base64-encoding.

public static String getFileContents(String docId) {
  return EncodingUtil.base64Encode(conVer.VersionData);

Back in the browser, you can use atob (ascii to binary) to recover the original binary data.

You must log in to answer this question.

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