-3

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

Drew Reese
  • 103,803
  • 12
  • 69
  • 96
  • 1
    Does this answer your question? https://stackoverflow.com/questions/54626186/how-to-download-file-with-javascript – Drew Reese May 16 '22 at 17:53

1 Answers1

0

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.

Yogesh
  • 66
  • 1
  • 4