Answer the question
In order to leave comments, you need to log in
How to make a link to download a file in react?
Good afternoon. There is a completely trivial task that was solved by native js like this
const download = filename, text) => {
let element = document.createElement('a');
element.setAttribute('href', 'somelink' + text);
element.setAttribute('download', filename);
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
const download = (filename, text) => {
return (
<a href={'somelink' + text} download={filename} />
)
Answer the question
In order to leave comments, you need to log in
Regarding your first method
In addition to download, always add target="_blank" + server upload with title
https://developer.mozilla.org/en/docs/Web/HTTP/%D0...
Instead of element.click(); create an event through createEvent because just .click() is not particularly cross-browser
https://stackoverflow.com/a/902838
Regarding the second, it's generally nonsense. Just use the first one.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question