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
🌐
Stack Overflow
stackoverflow.com › questions › 69380775 › choose-a-folder-where-the-file-will-be-downloadedreactjs
Choose a folder where the file will be downloaded(REACTJS)
How do I do this? Button is from Material UI ... You cannot do that. Browsers always allow the user to choose the download location, or else pre-configure it (like the "Downloads" directory).
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

🌐
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.
🌐
Reddit
reddit.com › r/reactnative › download files to downloads folder
r/reactnative on Reddit: Download files to Downloads folder
March 31, 2024 -

I'm struggling to figure out how to create a simple download button ti download files like pdf. In other apps with download feature (discord, slack, etc) you click a button which trigger a download. A notification shows up with download progress and the file ends up in the download folder. At least on Android.

Im using Expo and tried FileSystem.downloadAsync but it only seems to store in the app's own storage instead of the downloads folder and no notification is sent.

How is this usually achived in react native with Expo?

🌐
YouTube
youtube.com › watch
How to download files in React JS | Download file instead of opening in browser | React CSV Download - YouTube
#reactjs #download #CodeWithAamirIn this video tutorial I have explained how to download files in React JS application. Also explained how to download files ...
Published   January 4, 2023
🌐
Reddit
reddit.com › r/reactnative › implement file download to specific folder for android (rn-fetch-blob + react-native-document-picker)
r/reactnative on Reddit: Implement file download to specific folder for Android (rn-fetch-blob + react-native-document-picker)
October 30, 2023 -

Hello there,

I'm using a combo of rn-fetch-blob and react-native-document-picker to implement file download to specific folder for Android. Problem is that:

const { uri } = await DocumentPicker.pickDirectory();

uri is: ***"content://com.android.externalstorage.documents/tree/primary%3ADownload%2FTest"and rn-fetch-blob expects an actual folder path, something like: "/storage/emulated/0/Android/..."***Is there's a way to convert such uri to local path?

Thanks in advance

Update:There's similar issues on stackoverflow, no answers tho: https://stackoverflow.com/questions/76053310/how-to-pick-an-external-directory-and-read-its-content-in-react-native
https://stackoverflow.com/questions/76998259/documentpicker-pickdirectory-gives-the-content-uri-so-how-can-i-convert-with

🌐
GitHub
github.com › itinance › react-native-fs › issues › 200
Download file in downloads folder · Issue #200 · itinance/react-native-fs
November 5, 2016 - I need to downloaded file download in downloads folder of my device. When I downloaded file then it keeps in another place · 👍React with 👍13chmiguel, heeropunjabi, ZaidRehman, brunosousadev, leoroh and 8 more
Author   uc-asa
Top answer
1 of 16
146

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
104

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

🌐
CodeSandbox
codesandbox.io › s › download-files-in-react-33pmk
download files in react - CodeSandbox
April 6, 2021 - download files in react by V-ini-t86 using react, react-dom, react-download-link, react-scripts
Published   Apr 03, 2021
Author   V-ini-t86
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-download-pdf-file-in-reactjs
How To Download PDF file in ReactJS? | GeeksforGeeks
If such kind of file is stored in a publicly accessible folder, just create a hyperlink pointing to that file. When the user clicks the file link then the file will be downloaded.
Published   April 19, 2025
🌐
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!!

🌐
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> ); };
🌐
CopyProgramming
copyprogramming.com › howto › download-file-from-code-in-folder-reactjs
ReactJS: How to Download a File from a Folder using Code - Javascript
June 7, 2023 - Download sample file in public folder (react), To reference a file from inside the src folder, you have to access the public folder URL. React saves this path in an environment variable. To ... It is not possible for a frontend app running in a browser to automatically download and save a file to a specified location on a hard disk.
🌐
CopyProgramming
copyprogramming.com › howto › how-to-download-a-file-from-server-in-react
Reactjs: Downloading Files from a Server in React: A Guide
April 6, 2023 - To request a file without the file name, use the URL and write the callback function inside the express static. In a React application, you can use either link click or automatic download. If you need to create a file with data received from the response, you can create a blob object with the text and then create a Blob Object URL for that blob.
🌐
Syncfusion
syncfusion.com › forums › 174460 › file-folder-download
file/folder download | React - EJ 2 Forums | Syncfusion
April 18, 2022 - Should the beforeSend and success ... files or folders. I'm attempting to provide users with a progress indicator but these events don't seem to be firing for this operation. ... SIGN IN To post a reply. ... Greetings from Syncfusion support. In the File Manager, we have a beforeDownload event that will trigger before sending each download request. Check the below sample for reference. https://stackblitz.com/edit/react-8eugzo?fi...
🌐
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 - Make sure you check the application runs at http://localhost:3000/ by executing npm start on project directory react-file-download using cmd prompt. Note that when we open a file or edit a file in subsequent sections we will by default refer to the project root directory react-file-download and later we may not mention this directory name.
🌐
Quora
quora.com › How-can-I-code-in-JavaScript-to-download-my-files-to-a-specific-folder-where-I-want-to-save
How to code in JavaScript to download my files to a specific folder where I want to save - Quora
Answer (1 of 2): Short answer: You can’t. Long answer: Again, you can’t. I am not sure what you are trying to do here but I am assuming you are writing a script which will enable a browser user to click on one link and get a number of files downloaded, and that too to a local directory.
🌐
YouTube
youtube.com › watch
Download file in React JS | Download a File or Image using React - YouTube
How to Download PDF files in React JS. How to Download Images in React JS by clicking a button.
Published   February 16, 2023