DEV Community

Cover image for Why DigitalOcean Spaces Auto-Downloads Large PDFs - And How to Fix It
Tahsin Abrar
Tahsin Abrar

Posted on

Why DigitalOcean Spaces Auto-Downloads Large PDFs - And How to Fix It

If you're using DigitalOcean Spaces to serve static files like images, PDFs, or videos, you may have noticed something odd:
When you try to open a large PDF file (say 20MB+) using a direct URL, instead of showing a preview in the browser, it just downloads automatically.

Frustrating, right? Especially when you're expecting your users to view the file directly in their browser.

Today, weโ€™ll explore:

  • Why this happens
  • How headers like Content-Disposition affect behavior
  • And most importantly: How to fix it โ€” step by step

๐Ÿง The Problem: Browser Doesn't Preview Large PDF Files from DigitalOcean

Letโ€™s say your file lives at:

https://your-space-name.sgp1.cdn.digitaloceanspaces.com/sample.pdf
Enter fullscreen mode Exit fullscreen mode

If the file is small (like under 5MB), the browser might preview it normally.
But if it's a larger PDF (10MB, 20MB, or more), clicking that URL will force a download instead of previewing it.

Why? Two key reasons.


๐Ÿ” Root Cause #1: The Content-Disposition Header

When a file is served over HTTP, the server includes headers that tell the browser how to handle it. One important header is:

Content-Disposition: attachment
Enter fullscreen mode Exit fullscreen mode

That attachment part tells the browser:

โ€œHey, donโ€™t try to preview this โ€” just download it.โ€

If you want the file to open in-browser, the header must be:

Content-Disposition: inline
Enter fullscreen mode Exit fullscreen mode

So, if your uploaded file has attachment, no matter the file type, it will be downloaded.


๐Ÿ” Root Cause #2: CDN Behavior for Large Files

DigitalOcean Spaces sits on top of a CDN (Content Delivery Network).
For large files (like 10MB+), the CDN might default to a download mode to reduce bandwidth strain and caching complexity.

Itโ€™s not officially documented, but many developers have reported the same โ€” especially when uploading via third-party tools without specifying headers.


โœ… The Fix: Set Headers Correctly When Uploading

To ensure PDF preview works as expected:

โœ… Step 1: Set Content-Type and Content-Disposition

If you're using the AWS CLI (which also works with DigitalOcean Spaces), hereโ€™s how to upload:

aws s3 cp ./sample.pdf s3://your-space-name/ \
  --endpoint-url https://sgp1.digitaloceanspaces.com \
  --content-type application/pdf \
  --content-disposition inline
Enter fullscreen mode Exit fullscreen mode

This ensures:

  • Your file is marked as a PDF
  • The browser knows it can preview it (inline)

โš ๏ธ If you've already uploaded the file, youโ€™ll need to re-upload or use the SDK to update the metadata.


โœ… Step 2: Verify the Headers

After uploading, open DevTools โ†’ Network tab and click your file URL. Check the response headers:

  • โœ… Content-Type: application/pdf
  • โœ… Content-Disposition: inline

If those are in place, your browser should now preview the file, no matter its size.

Top comments (0)