tldr; fetch the file from the url, store it as a local Blob, inject a link element into the DOM, and click it to download the Blob

I had a PDF file that was stored in S3 behind a Cloudfront URL. I wanted the user to be able to click a button and immediately initiate a download without popping open a new tab with a PDF preview. Generally, if a file is hosted at a URL that has a different domain that the site the user is currently on, immediate downloads are blocked by many browsers for user security reasons. If you use this solution, do not initiate the file download unless a user clicks on a button to intentionally download.

In order to get by this, I needed to fetch the file from the URL getting around any CORS policies to save a local Blob that would then be the source of the downloaded file. In the code below, make sure you swap in your own fileURL, Content-Type, and FileName.

fetch('https://cors-anywhere.herokuapp.com/' + fileURL, {
    method: 'GET',
    headers: {
      'Content-Type': 'application/pdf',
    },
  })
  .then((response) => response.blob())
  .then((blob) => {
    // Create blob link to download
    const url = window.URL.createObjectURL(blob);

    const link = document.createElement('a');
    link.href = url;
    link.setAttribute(
      'download',
      `FileName.pdf`,
    );

    // Append to html link element page
    document.body.appendChild(link);

    // Start download
    link.click();

    // Clean up and remove the link
    link.parentNode.removeChild(link);
  });

This solution references solutions to getting a blob from a URL and using a CORS proxy.

Update As of January 31st, 2021, the cors-anywhere demo hosted on Heroku servers will only allow limited use for testing purposes and cannot be used for production applications. You will have to host your own cors-anywhere server by following cors-anywhere or cors-server.

Answer from Brian Li on Stack Overflow
Top answer
1 of 16
149

tldr; fetch the file from the url, store it as a local Blob, inject a link element into the DOM, and click it to download the Blob

I had a PDF file that was stored in S3 behind a Cloudfront URL. I wanted the user to be able to click a button and immediately initiate a download without popping open a new tab with a PDF preview. Generally, if a file is hosted at a URL that has a different domain that the site the user is currently on, immediate downloads are blocked by many browsers for user security reasons. If you use this solution, do not initiate the file download unless a user clicks on a button to intentionally download.

In order to get by this, I needed to fetch the file from the URL getting around any CORS policies to save a local Blob that would then be the source of the downloaded file. In the code below, make sure you swap in your own fileURL, Content-Type, and FileName.

fetch('https://cors-anywhere.herokuapp.com/' + fileURL, {
    method: 'GET',
    headers: {
      'Content-Type': 'application/pdf',
    },
  })
  .then((response) => response.blob())
  .then((blob) => {
    // Create blob link to download
    const url = window.URL.createObjectURL(blob);

    const link = document.createElement('a');
    link.href = url;
    link.setAttribute(
      'download',
      `FileName.pdf`,
    );

    // Append to html link element page
    document.body.appendChild(link);

    // Start download
    link.click();

    // Clean up and remove the link
    link.parentNode.removeChild(link);
  });

This solution references solutions to getting a blob from a URL and using a CORS proxy.

Update As of January 31st, 2021, the cors-anywhere demo hosted on Heroku servers will only allow limited use for testing purposes and cannot be used for production applications. You will have to host your own cors-anywhere server by following cors-anywhere or cors-server.

2 of 16
105

This is not related to React. However, you can use the download attribute on the anchor <a> element to tell the browser to download the file.

<a href='/somefile.txt' download>Click to download</a>

This is not supported on all browsers: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a

🌐
Learnreactui
learnreactui.dev › contents › how-to-download-a-file-in-react
How To Download a File in React
The code is quite simple. We create a DownloadLink component. This component gives the URL of the file to be downloaded and the name of the file to be saved as a file when saving it locally.
🌐
DEV Community
dev.to › farisdurrani › how-to-download-a-file-in-a-full-stack-react-app-2jkk
How to download a file in a full-stack React app - DEV Community
July 23, 2023 - Today, we'll learn how to download a file from the backend server to be downloaded in the client server.
🌐
Roy Tutorials
roytuts.com › home › javascript › react js › download file from server using react
Download file from server using React - Roy Tutorials
November 15, 2025 - Open the file public/index.html and update the title tag as shown below: ... Create DownloadFile.js file under src directory with below content.
🌐
JavaScript in Plain English
javascript.plainenglish.io › how-to-download-files-on-button-click-reactjs-f7257e55a26b
How to Create a Download Button Using React | JavaScript in Plain English
July 16, 2024 - // file object const file = new Blob(texts, {type: 'text/plain'});// anchor link const element = document.createElement("a"); element.href = URL.createObjectURL(file); element.download = "100ideas-" + Date.now() + ".txt";// simulate link click document.body.appendChild(element); // Required for this to work in FireFox element.click(); }
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › how-to-download-pdf-file-in-reactjs
How To Download PDF file in ReactJS? - GeeksforGeeks
Downloading files, such as PDFs, in a React application can be easily achieved using two main methods: Using the HTML DOM Anchor Object and using the fetch() Method. Both of these approaches allow you to download files and store them locally, ...
Published   July 23, 2025
🌐
Bobby Hadz
bobbyhadz.com › blog › react-download-file
How to download a File in React.js (local or from URL) | bobbyhadz
April 7, 2024 - A step-by-step illustrated guide on how to download a local or a remote file in React.js.
🌐
npm
npmjs.com › package › react-use-downloader
react-use-downloader - npm
June 15, 2025 - 'in progress' : 'stopped'}</p> <button onClick={() => download(fileUrl, filename)}> Click to download the file </button> <button onClick={() => cancel()}>Cancel the download</button> <p>Download size in bytes {size}</p> <label for="file">Downloading ...
      » npm install react-use-downloader
    
Published   Jun 15, 2025
Version   1.3.0
Author   Olavo Parno
Find elsewhere
🌐
Reddit
reddit.com › r/react › a simple way to download a file
r/react on Reddit: A Simple Way To Download A File
August 24, 2023 -

Is there a simple way to download a .pdf or .txt file I'm building a React/Express/MySQL web app that uses Puppeteer to navigate a website, login using the users' provided username and password, and click a few buttons on the site in order to get the site to generate a . pdf file and .txt file (the site creates links to download these files, when clicking the links in a normal browser a small dialogue window appears, clicking OK in this window triggers the download of the .pdf or .txt).

When I have Puppeteer dismiss the window, a bunch of strange files and folders are created in my ./downloads folder (my downloads path) but the .pdf doesn't download. Is there a simple way or library I can install and use to handle downloading these .pdf or .txt files? Thank you!!

🌐
JavaScript in Plain English
javascript.plainenglish.io › download-pdf-from-api-in-reactjs-using-axios-and-blobs-699be8a27ca7
Download PDF from API in React (Using Axios and blobs) | by Subhanu | JavaScript in Plain English
December 11, 2024 - Create a new ReactJS project or navigate to your existing project directory. Open a terminal and run the following command to install axios: ... 4. Next, create a basic file structure for your project. We can organize your files in a way that suits your needs, but here’s a simple example: src/ ├── components/ │ └── PDFDownloader.js └── App.js · In this example, we have a FileDownloader component that will handle the PDF downloading functionality.
🌐
GitHub
github.com › axetroy › react-download
GitHub - axetroy/react-download: react component for click and then download the specify content file. · GitHub
import React from 'react'; import { render } from 'react-dom'; import Download from '@axetroy/react-download'; const element = document.createElement('div'); document.body.appendChild(element); class App extends React.Component { render() { ...
Starred by 36 users
Forked by 8 users
Languages   JavaScript 96.2% | HTML 3.8%
🌐
npm
npmjs.com › package › react-file-download
react-file-download - npm
Latest version: 0.3.5, last published: 8 years ago. Start using react-file-download in your project by running `npm i react-file-download`. There are 30 other projects in the npm registry using react-file-download.
      » npm install react-file-download
    
Published   Sep 14, 2017
Version   0.3.5
Author   Kenneth Jiang
🌐
Filestack
blog.filestack.com › home › the ultimate guide to file downloads in react using apis
The Ultimate Guide to File Downloads in React Using APIs
January 16, 2025 - Learn how to handle file downloads in React using APIs, with patterns for file types, UX, security, and smoother download experiences online.
🌐
npm
npmjs.com › package › react-download-link
react-download-link - npm
March 16, 2020 - filename="myfile.txt" exportFile={() => Promise.resolve("My cached data")} /> The component will default to an anchor tag, but the tagName prop will accept a string of any other HTML tag you prefer, such as 'button'. react · react.js · react-component · npm i react-download-link ·
      » npm install react-download-link
    
Published   Mar 16, 2020
Version   2.3.0
Author   Peter Moresi
🌐
Filestack
blog.filestack.com › home › react download file from api: a guide
React download file from API: A Guide
July 19, 2023 - For instance, you can download files via direct links or using API endpoints. The problem with downloading a file via a URL/direct link is that we first have to generate the file on the server side, and once the file is ready, we can provide ...
🌐
Thelearning
thelearning.dev › how-to-download-files-on-button-click-reactjs
How to Download Files on Button Click ReactJS?
Professional corporate training in Python, Data Engineering, AI Agents, Apache Airflow by Bhavani Ravi
🌐
Medium
medium.com › @kbhattacharya75 › download-text-file-from-a-website-using-only-javascript-react-958ce41be593
Create and Download file from a Browser using only JavaScript/React | by Kaustav Bhattacharya | Medium
April 25, 2023 - <button onClick={()=>handleDownload('file Name','Hello World')}> Download Text File </button> In my case I jsonified the data using JSON.stringify(data) Press enter or click to view image in full size · So there you have it, use it and follow for more · JavaScript · Download · React · Reactjs ·
🌐
Medium
medium.com › yellowcode › download-api-files-with-react-fetch-393e4dae0d9e
Download API Files With React & Fetch | by Manny | yellowcode | Medium
April 11, 2020 - This method is the one we’ll be looking at today, where the file data is sent to us via the API, we interpret that data, and download it directly on the client side, without opening up a new tab.
🌐
GitHub
gist.github.com › prestigegodson › 8f5ccd46399dc5e5844da8af810a5e9e
React source code to download file via ajax · GitHub
React source code to download file via ajax. GitHub Gist: instantly share code, notes, and snippets.