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 Overflowtldr; 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.
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
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?
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
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.
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
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!!
The tag defines a link between a document and an external resource. To let download funcionality on your website you should change link tag
import chargeSample from './public/chargeSample.xlsx';
<Button><a href={chargeSample} download="your file name">Download</a></Button>
If you are using creat react app, you can pass public path into your components at build time by using
process.env.PUBLIC_URL