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
🌐
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
🌐
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
Discussions

A Simple Way To Download A File
I’m guessing that the file is being served to puppeteer as a Blob. You need to create a object URL to the Blob stream, then make puppeteer visit that object URL then write the stream to a file. Here’s an example: https://stackoverflow.com/questions/65503045/how-to-download-pdf-blob-using-puppeteer Further reading: https://developer.mozilla.org/en-US/docs/Web/API/Blob https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL_static More on reddit.com
🌐 r/react
2
1
August 24, 2023
A Simple Way To Download A File : react
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... More on beta.reddit.com
🌐 r/react
How to generate + download file on the fly? Django/React
You don't need to store the file on the backend as it can be created in memory. The docs have some pretty good examples: https://docs.djangoproject.com/en/4.1/howto/outputting-csv/ Alternatively, you can use io.StringIO() as an in-memory buffer to generate the CSV on the fly then output the buffer value in your response. More on reddit.com
🌐 r/django
3
9
November 22, 2022
WSL 2: React not reloading with file changes
They changed the file sharing protocol, from using they own custom developed protocol to using the 9P protocol, which at this time might not support file changes event. I believe you can fix this issue by putting your code on the Linux file system (ex: in your user's home directory), and access these files through the WSL share, \\wsl$\DISTRO_NAME from Windows. In the Windows explorer, if you go to \\wsl$, you should see all your WSL Linux distros installed and can access all the files on their file systems. More on reddit.com
🌐 r/bashonubuntuonwindows
4
18
June 23, 2019
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

🌐
npm
npmjs.com › package › react-download-link
react-download-link - npm
March 16, 2020 - Latest version: 2.3.0, last published: 6 years ago. Start using react-download-link in your project by running `npm i react-download-link`. There are 2 other projects in the npm registry using react-download-link.
      » npm install react-download-link
    
Published   Mar 16, 2020
Version   2.3.0
Author   Peter Moresi
🌐
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 - See A simple React Next + Node app in Typescript. First, install axios in frontend/. We're using axios instead of the native fetch to post the file since we want to print the download progress. npm i axios · We will also install downloadjs to download a blob to the hard disk: npm i downloadjs @types/downloadjs ·
🌐
npm
npmjs.com › package › react-downloader-file
react-downloader-file - npm
September 23, 2020 - import React from 'react'; import DownLoadFile,{DownloadFileProps} from 'react-downloader-file'; const ExampleComponent:React.FC<DownloadFileProps> = props =>{ return ( <DownLoadFile url="/user/info" headerParams={{ method:'POST', ...
      » npm install react-downloader-file
    
Published   Sep 23, 2020
Version   1.0.0
Author   lishuai
🌐
Roy Tutorials
roytuts.com › home › react js › download file from server using react
Download file from server using React - Roy Tutorials
November 15, 2025 - Make sure to give the project name as react-file-download. Wait till the project directory creation is not finished. When done you will get successful message. Make sure you check the application runs at http://localhost:3000/ by executing npm ...
Find elsewhere
🌐
npm
npmjs.com › package › react-download
react-download - npm
October 23, 2015 - File download component for React. Latest version: 0.0.1, last published: 10 years ago. Start using react-download in your project by running `npm i react-download`. There are no other projects in the npm registry using react-download.
      » npm install react-download
    
Published   Oct 23, 2015
Version   0.0.1
🌐
npm
npmjs.com › search
file-download - npm search
download · file · url · get · http · https · montanaflynn• 0.1.5 • 11 years ago • 95 dependents • ISCpublished version 0.1.5, 11 years ago95 dependents licensed under $ISC · 32,005 · Javascript function that triggers browser to save javascript-generated content to a file · javascript · file · react ·
🌐
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-native-file-download
react-native-file-download - npm
May 29, 2016 - Start using react-native-file-download in your project by running `npm i react-native-file-download`. There are no other projects in the npm registry using react-native-file-download.
      » npm install react-native-file-download
    
Published   May 29, 2016
Version   0.0.10
Author   Perry Poon
🌐
npm
npmjs.com › package › js-file-download
js-file-download - npm
May 6, 2020 - npm install js-file-download --save · var fileDownload = require('js-file-download'); fileDownload(data, 'filename.csv'); javascript · file · react · download · npm i js-file-download · github.com/kennethjiang/js-file-download · ...
      » npm install js-file-download
    
Published   May 06, 2020
Version   0.4.12
Author   Kenneth Jiang
🌐
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.
🌐
GitHub
github.com › jonashaag › react-file-download
GitHub - jonashaag/react-file-download
React component to trigger browser to save data to file as if it was downloaded. npm install react-file-download --save ·
Author   jonashaag
🌐
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 - Here’s how we can integrate file downloads into our React app using the file-saver library: Install the file-saver library: npm install file-saver · Set up the file download functionality: Let’s create a function that handles file downloads ...
🌐
npm
npmjs.com › package › react-file-downloader
react-file-downloader - npm
November 26, 2019 - Just install, import with import { downloadFile } from 'react-file-downloader' and call downloadFile. The method accepts 2 arguments, URL and file name.
      » npm install react-file-downloader
    
Published   Nov 26, 2019
Version   1.0.4
Author   Ali Abji
🌐
Learnreactui
learnreactui.dev › contents › how-to-download-a-file-in-react
How To Download a File in React
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. import { Button } from "antd"; import React from "react"; export const DownloadLink = ({ url, fileName }) => { const handleDownload = () => { fetch(url) .then((response) => response.blob()) .then((blob) => { const url = window.URL.createObjectURL(new Blob([blob])); const link = document.createElement("a"); link.href = url; link.download = fileName || "downloaded-file"; document.body.appendChild(link); link.click(); document.body.removeChild(link); window.URL.revokeObjectURL(url); }) .catch((error) => { console.error("Error fetching the file:", error); }); }; return ( <div> <Button type="primary" onClick={handleDownload}> Download Sample JSON </Button> </div> ); };
🌐
React-cn
react-cn.github.io › react › downloads.html
Downloads | React
The uncompressed, development version of react.js and react-dom.js with inline documentation (you need both files).
🌐
Npm
npm.io › package › react-file-download
React-file-download NPM | npm.io
React component to trigger browser to save data to file as if it was downloaded.