🎤 Builder Talk: The Story Behind Lady Gaga’s Digital Experience – Register now

Implementing PDF file download using JavaScript and resolving CORS policy error

3 replies
Last updated: Dec 27, 2021
Hey everyone, I am going to implement PDF file download using javascript and here's the code snippets. But I am getting CORS policy error. Any idea?
  const download = (url, name, redirectTo) => {
    if (!url) {
      throw new Error("Resource URL not provided! You need to provide one");
    }
    fetch(url)
      .then(response => response.blob())
      .then(blob => {
        setFetching(false);
        const blobURL = URL.createObjectURL(blob);
        const a = document.createElement("a");
        a.href = blobURL;
        a.style = "display: none";

        if (name && name.length) a.download = name;
        document.body.appendChild(a);
        a.click();
				navigate(redirectTo)
      })
      .catch(() => setErrors(["Something went wrong in fetching file data for download."]));
  };
Dec 27, 2021, 8:32 PM
user A
Great! thank you. I didn't know this great feature on sanity management board. It's working now.
Dec 27, 2021, 11:05 PM
user A
Great! thank you. I didn't know this great feature on sanity management board. It's working now.
Dec 27, 2021, 11:05 PM
Great! I’m glad things are working for you. 🙌
Dec 27, 2021, 11:06 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
Hi all! Does anyone have problem on deoplloying on Vercel (Using Next. js as a frontend)? I have two test projects, andonce...Jan 25, 2021
Filtering out duplicate slugs after a large data importSep 8, 2020
Modifying a string value via a callback in JavaScriptJun 16, 2020
How to perform a "publish" through the API/JavaScript client in Sanity.ioJul 21, 2020
I’m experiencing an error when trying to deploy `sanity graphql deploy` and getting this error point towards a node_module ```import...Jan 16, 2021
Gatsby site crashing with cryptic error, resolved by adjusting resolveReferences depthAug 18, 2020
Browsing raw data and wiping all data in a Sanity instance discussed.Apr 28, 2020
Troubleshooting deployment and login issues with self-hosted Sanity instanceFeb 23, 2022
Updating object properties in an array on a document with the Sanity clientMar 17, 2021
Issue with CORS error when fetching data from Sanity Studio in React AppSep 1, 2021

Related contributions

Turbo Start Sanity
- Template

The battle-tested Sanity template that powers Roboto Studio's websites

Go to Turbo Start Sanity

Schema UI - Next.js Sanity Starter
- Template

A Next.js starter template with Next.js 15, Tailwind CSS, shadcn/ui, and Sanity CMS with Live Editing. Get production-ready React components with matching Sanity schemas and queries. Build dynamic pages faster while keeping full control over customization.

Serge Ovcharenko
Go to Schema UI - Next.js Sanity Starter

The Swaddle
- Made with Sanity

A new brand identity to represent a more mature company, to signify The Swaddle’s evolution from publisher to production house, combined with an easier to navigate platform that can surface multiple content types - drawing readers through The Swaddle’s content offering.

Nightjar
Go to The Swaddle