Download text file when cliking link in React

Viewed 78

Normally I can use this line to trigger the download of files:

<a href={require("../path/to/file.pdf")} download="myFile">Download file</a>

but when the file is plain text, for example a .txt file, when I click on the link the file is just opened in the browser to see the contents, is there a way to trigger the download of text files too?

2 Answers

the way u could do it like this:

<a href="testFile.txt" download rel="noopener noreferrer" target="_blank">
    Download File
 </a>

Remember that the dowload attribute is not supported by IE11. This why we need a target="_blank".

<a href={require("../path/to/file.pdf")} target="_blank">Download file</a>
Related