'Download file stored in public folder of react [closed]

hi I wanted to download a file programmatically by clicking on function. File is stored in public folder user react router Dom v6 but could not find any help in internet



Solution 1:[1]

First Let me correct you. You want to download a file when you click a button.

So to do that you can do like this : <button onClick={tempfunc}>Click me to Download File</button>

Now Create tempfunc Function Like this :

function tempfunc(){
    const filepath = "/logo512.png";   // Path of you file. Remember / points to public folder
    const a = document.createElement('a');
    a.href = filepath;
    a.setAttribute("download","")  // you can set name of you file to Download as. Like my file original name is logo512.png but if i want to download it as "somethis.png" then I will set download value (second Parameter) as "something.png"
    a.click();
  } 

I Hopy this will help you.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yogesh