Why Does This <a href download> HTML Redirect to File Instead of Downloading?

1.1k views Asked by At

Here is the html:

<!DOCTYPE HTML>
<html>
    <body>
    <a href = "img/test.txt" download> Download </a>
    </body>
</html>

This identifies the file that I am trying to download, but instead of downloading it like I expect the "download" attribute to do, it just redirects to the txt file within the browser. What am I doing wrong here?

1

There are 1 answers

0
Nikhil Singh On BEST ANSWER

This is how you download the file, (React env, as you mentioned in the chat)

import txtFile from '<path_to_txt_file>';

// putting this inside the React component
<a href={txtFile} download="txtFile.txt"> Download Here </a>;