Search code examples
javascriptreactjsanchor

Anchor (a) tag download file not working, using React


I want to be able to download a file named approved_leads.zip when clicking on the a tag, but for some reason I keep getting the message "failed - no file". Even though the path looks correct to me.

<a href="../../assets/approved_leads.zip" download>Download File</a>

And, here's the folder structure of it, right now the a tag is inside the jsx file called "LeadsPage.jsx" and the .zip file inside the assets folder.

enter image description here


Solution

  • You need to import the file that you want to be downloadable. React do not accept this perspective. You can do it like that:

    First, import the file with any name,

    import MyDownloadableFile from "../../assets/approved_leads.zip"
    

    Secondly, you need to give the reference of file to a tag,

    <a href={MyDownloadableFile} download>Download File</a>