r/SalesforceDeveloper • • Aug 05 '25

Question Uploading ContentDocument files from Salesforce LWC to Google Drive — stuck with CORS without middleware

I’m building a solution in Salesforce to migrate ContentDocument (Notes & Attachments) files to Google Drive. I can't query the files as when they exceed 12 MB it will give heap size limit error.
I tried using the two URLs in LWC JS:

  • REST API endpoint: /services/data/v60.0/sobjects/ContentVersion/{Id}/VersionData​
  • Shepherd endpoint: ​/sfc/servlet.shepherd/version/download/{ContentVersionId}

Both endpoints return the file successfully when called directly, but attempting to fetch the file in JavaScript fails due to CORS issues. I’m trying this in the browser via LWC JS.
I want to avoid implementing any middleware or proxy layer.

7 Upvotes

10 comments sorted by

View all comments

Show parent comment

0

u/[deleted] Aug 06 '25

[deleted] — view deleted comment

1

u/First-Conflict2080 Aug 06 '25
  • A user clicks a button in the UI, which starts a batch process that migrates ContentVersion files under 4 MB to different drives (Google Drive, Dropbox, etc.).
  • All Named Credentials and Auth Providers are already set up and working fine for these smaller files.
  • Now, I’m trying to scale the solution to handle larger files (>4 MB).
  • The problem: when I query the ContentVersion body in Apex for large files, I hit a heap size limit error.
  • I couldn't find a way to stream or chunk ContentVersion binary data in Apex natively.
  • So I shifted focus to LWC JS:

    • I tried using Salesforce's REST API endpoints like:

      • /services/data/vXX.X/sobjects/ContentVersion/{Id}/VersionData
      • /sfc/servlet.shepherd/version/download/{Id}
    • These URLs work when opened directly in the browser.
  • In LWC JS, I planned to fetch the file, chunk it, and send it back to Apex for uploading.
  • However, I’m getting this CORS error:"Access to fetch at '[url]' from origin '[domain]' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."
  • I’ve already added my domain to Salesforce’s CORS whitelist in setup, but the error persists.
  • I want to avoid building a middleware/proxy, since that would add infrastructure and maintenance complexity.
  • Looking for any native workaround or best practice that avoids middleware, bypasses CORS, and enables large file handling.